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_idproduct_namecategorypricestock
1Wireless MouseElectronics120025
2Yoga MatFitness80050
3Bluetooth SpeakerElectronics250015
4Water BottleFitness300100
5SmartwatchElectronics500010

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.

SELECT – The Backbone of Queries

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.

SELECT product_name, price  
FROM products;  

Result:

product_nameprice
Wireless Mouse1200
Yoga Mat800
Bluetooth Speaker2500
Water Bottle300
Smartwatch5000

FROM – Tell SQL Where to Look

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.

WHERE – Filter Your Data

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';  

Result:

product_nameprice
Wireless Mouse1200
Bluetooth Speaker2500
Smartwatch5000

ORDER BY – Sorting Your Data

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;  

Result:

product_nameprice
Wireless Mouse1200
Bluetooth Speaker2500
Smartwatch5000

You can also sort in descending order with DESC.

TOP or LIMIT – Control How Much Data You Get

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:

Result:

product_nameprice
Wireless Mouse1200
Bluetooth Speaker2500

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;  

Result:

NamePrice
Wireless Mouse1200
Yoga Mat800
Bluetooth Speaker2500
Water Bottle300
Smartwatch5000

DISTINCT – Remove Duplicates

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?

SELECT DISTINCT category  
FROM products;  

Result:

category
Electronics
Fitness

Combining Clauses for Complex Queries

Let’s put it all together with a slightly complex query.

Scenario:

You want to find the top 2 most expensive products in the “Fitness” category.

SELECT product_name, price  
FROM products  
WHERE category = 'Fitness'  
ORDER BY price DESC  
LIMIT 2;  

Result:

product_nameprice
Yoga Mat800
Water Bottle300

Final Thoughts

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.

Was this page helpful?