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
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
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
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
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
.
product_name | price |
---|---|
Wireless Mouse | 1200 |
Bluetooth Speaker | 2500 |
AS
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
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 |
product_name | price |
---|---|
Yoga Mat | 800 |
Water Bottle | 300 |
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.