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 |
demo_db
. Now, letâs use this table to explore SQL basics.
SELECT â The Backbone of Queries
TheSELECT
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.
product_name | price |
---|---|
Wireless Mouse | 1200 |
Yoga Mat | 800 |
Bluetooth Speaker | 2500 |
Water Bottle | 300 |
Smartwatch | 5000 |
FROM â Tell SQL Where to Look
TheFROM
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
TheWHERE
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?
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 theORDER BY
clause.
Example: Letâs sort the electronics products by price, in ascending order.
product_name | price |
---|---|
Wireless Mouse | 1200 |
Bluetooth Speaker | 2500 |
Smartwatch | 5000 |
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: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. UseAS
to create an alias.
Example:
What if you want to rename the product_name
column to just Name
?
Name | Price |
---|---|
Wireless Mouse | 1200 |
Yoga Mat | 800 |
Bluetooth Speaker | 2500 |
Water Bottle | 300 |
Smartwatch | 5000 |
DISTINCT â Remove Duplicates
TheDISTINCT
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?
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.product_name | price |
---|---|
Yoga Mat | 800 |
Water Bottle | 300 |
Final Thoughts
TheSELECT
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.