This guide explains common SQL queries using simple users and orders tables.
| user_id | name | |
|---|---|---|
| 1 | Alice | alice@gmail.com |
| 2 | Bob | bob@gmail.com |
| 3 | Alice | alice2@gmail.com |
| order_id | user_id | product_name | amount |
|---|---|---|---|
| 101 | 1 | Laptop | 75000 |
| 102 | 1 | Mouse | 2000 |
| 103 | 2 | Mobile | 25000 |
| 104 | 4 | Tablet | 30000 |
Used to fetch all columns from a table.
SELECT * FROM users;Output
| user_id | name | |
|---|---|---|
| 1 | Alice | alice@gmail.com |
| 2 | Bob | bob@gmail.com |
| 3 | Alice | alice2@gmail.com |
SELECT name, email FROM users;Output
| name | |
|---|---|
| Alice | alice@gmail.com |
| Bob | bob@gmail.com |
| Alice | alice2@gmail.com |
Filters rows based on condition.
SELECT * FROM orders WHERE amount > 30000;Output
| order_id | user_id | product_name | amount |
|---|---|---|---|
| 101 | 1 | Laptop | 75000 |
Removes duplicate values.
SELECT DISTINCT name FROM users;Output
| name |
|---|
| Alice |
| Bob |
SELECT DISTINCT name FROM users;| name |
|---|
| Alice |
| Bob |
- The
DISTINCTkeyword removes duplicate values from the selected column. - In the
userstable, Alice appears twice, butDISTINCTensures it is shown only once. - The database first looks at all
namevalues, removes duplicates, then returns the result.
Sorts data (ASC by default).
SELECT * FROM orders ORDER BY amount DESC;Output
| order_id | product_name | amount |
|---|---|---|
| 101 | Laptop | 75000 |
| 104 | Tablet | 30000 |
| 103 | Mobile | 25000 |
| 102 | Mouse | 2000 |
Restricts number of rows returned.
SELECT * FROM orders LIMIT 2;Output
| order_id | product_name | amount |
|---|---|---|
| 101 | Laptop | 75000 |
| 102 | Mouse | 2000 |
Counts number of rows.
SELECT COUNT(*) AS total_orders FROM orders;Output
| total_orders |
|---|
| 4 |
Calculates total of a column.
SELECT SUM(amount) AS total_amount FROM orders;Output
| total_amount |
|---|
| 132000 |
Groups rows for aggregation.
SELECT name, COUNT(user_id) AS total_users
FROM users
GROUP BY name;Output
| user_name | total_users |
|---|---|
| Alice | 2 |
| Bob | 1 |
Filters grouped data (used with aggregates).
SELECT user_id, SUM(amount) AS total_spent
FROM orders
GROUP BY user_id
HAVING SUM(amount) >= 30000;Output
| user_id | total_spent |
|---|---|
| 1 | 77000 |
| 4 | 30000 |
👉 Note:
WHERE→ filters rowsHAVING→ filters aggregated results
Returns only matching records.
SELECT u.name, o.product_name, o.amount
FROM users u
INNER JOIN orders o
ON u.user_id = o.user_id;Output
| name | product_name | amount |
|---|---|---|
| Alice | Laptop | 75000 |
| Alice | Mouse | 2000 |
| Bob | Mobile | 25000 |
All rows from left table + matching right.
SELECT u.name, o.product_name
FROM users u
LEFT JOIN orders o
ON u.user_id = o.user_id;Output
| name | product_name |
|---|---|
| Alice | Laptop |
| Alice | Mouse |
| Bob | Mobile |
| Alice | NULL |
All rows from right table + matching left.
SELECT u.name, o.product_name
FROM users u
RIGHT JOIN orders o
ON u.user_id = o.user_id;Output
| name | product_name |
|---|---|
| Alice | Laptop |
| Alice | Mouse |
| Bob | Mobile |
| NULL | Tablet |
Updates existing data.
UPDATE users
SET email = 'alice_new@gmail.com'
WHERE user_id = 1;✔ Alice’s email is updated.
Deletes selected rows.
DELETE FROM orders WHERE order_id = 102;✔ Mouse order deleted.
Deletes all rows, keeps table structure.
TRUNCATE TABLE orders;✔ Fast and irreversible.
Deletes table permanently.
DROP TABLE users;❌ Table + data removed forever.
- SELECT → fetch data
- WHERE → filter rows
- DISTINCT → remove duplicates
- GROUP BY → aggregate data
- HAVING → filter aggregated data
- JOIN → combine multiple tables
- TRUNCATE vs DELETE → structure kept vs row-based
- DROP → removes table completely
SELECT customer_id
FROM orders
WHERE order_date >= DATE '2024-01-01'
AND order_date < DATE '2025-01-01'
GROUP BY customer_id
HAVING SUM(amount) > 10000;