Read data in SQL
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.
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.
Result:
product_name | price |
---|---|
Wireless Mouse | 1200 |
Yoga Mat | 800 |
Bluetooth Speaker | 2500 |
Water Bottle | 300 |
Smartwatch | 5000 |
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?
Result:
product_name | price |
---|---|
Wireless Mouse | 1200 |
Bluetooth Speaker | 2500 |
Smartwatch | 5000 |
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.
Result:
product_name | price |
---|---|
Wireless Mouse | 1200 |
Bluetooth Speaker | 2500 |
Smartwatch | 5000 |
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_name | price |
---|---|
Wireless Mouse | 1200 |
Bluetooth Speaker | 2500 |
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
?
Result:
Name | Price |
---|---|
Wireless Mouse | 1200 |
Yoga Mat | 800 |
Bluetooth Speaker | 2500 |
Water Bottle | 300 |
Smartwatch | 5000 |
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?
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.
Result:
product_name | price |
---|---|
Yoga Mat | 800 |
Water Bottle | 300 |
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?