Use this file to discover all available pages before exploring further.
So, youâve dipped your toes into SQL 101 and now want to start querying your database. The SELECT statement is where all the magic begins. Itâs your way of asking a database, âHey, give me this specific data.â Today, weâll go through the basics of the SELECT statement and its accompanying clauses, step by step, with one consistent example to make it all crystal clear.Imagine This:Youâre managing a small online store. Your database has a table called products that looks something like this:
product_id
product_name
category
price
stock
1
Wireless Mouse
Electronics
1200
25
2
Yoga Mat
Fitness
800
50
3
Bluetooth Speaker
Electronics
2500
15
4
Water Bottle
Fitness
300
100
5
Smartwatch
Electronics
5000
10
We will see later in this series how to create table like this and insert the data. Just execute the below queries for now in the demo_db. Now, letâs use this table to explore SQL basics.
The SELECT statement is how you choose which columns of data to fetch from your table. Think of it as selecting the fields you want to see.Example: Letâs say you want to see the names and prices of all your products.
The FROM clause specifies the table to query. In the example above, weâre telling SQL to look at the products table. Without the FROM clause, SQL wouldnât know where to pull the data from.
The WHERE clause is like setting a condition. You donât want all the data, just specific rows that match your criteria.Example: What if you want to see all the products in the âElectronicsâ category?
SELECT product_name, price FROM products WHERE category = 'Electronics';
Want to see your results sorted in a specific order? Use the ORDER BY clause.Example: Letâs sort the electronics products by price, in ascending order.
SELECT product_name, price FROM products WHERE category = 'Electronics'ORDER BY price ASC;
The or clause is like saying, âJust show me the first few rows.â There are some difference in the implementation of LIMIT across database management systems. Read this to understand the SQL dialects and the differences.Example: Letâs say you only want the two cheapest electronics products:
SELECT TOP 2 product_name, price FROM products WHERE category = 'Electronics'ORDER BY price ASC;
Aliasing with AS â Give Columns or Tables a Friendly Name
Sometimes, column names are too long or not reader-friendly. Use AS to create an alias.Example:What if you want to rename the product_name column to just Name?
SELECT product_name AS Name, price AS Price FROM products;
The DISTINCT keyword ensures you only see unique values in a column. It just shows you the unique records. It does not remove the duplicate from the table. As we are only reading from the table in SELECT clause.Example:What if you want to see all the unique categories in your table?
The SELECT statement is the foundation of SQL queries, and with these basic clausesâFROM, WHERE, ORDER BY, LIMIT, DISTINCT, and ASâyouâre equipped to start exploring your database. Stick to one example like our products table as you practice these commands. Once youâre comfortable, try using these queries on your own datasets.SQL isnât just about writing queries; itâs about asking questions and letting the database answer. And now, youâve taken the first big step in learning how to ask those questions effectively.Next time, weâll dive into aggregations âlearning how to calculate totals, averages, and more using SQL.