Whether you are prepping for a technical interview or building your first database-driven app, SQL (Structured Query Language) is the universal language of data.
While SQL can get complex, 90% of your daily tasks will involve a handful of core commands. This cheat sheet serves as your quick-reference guide to the most common queries.
1. The Basics: Querying Data
The most common task in SQL is retrieving information from a table.
- Select all columns:
SELECT * FROM table_name; - Select specific columns:
SELECT column1, column2 FROM table_name; - Select unique values (no duplicates):
SELECT DISTINCT column1 FROM table_name;
2. Filtering Results
Narrow down your data using the WHERE clause.
| Operator | Description | Example |
= | Equal to | WHERE age = 25 |
!= or <> | Not equal to | WHERE status != 'active' |
LIKE | Pattern matching | WHERE name LIKE 'J%' (Starts with J) |
IN | Multiple possible values | WHERE country IN ('USA', 'UK') |
BETWEEN | Within a range | WHERE price BETWEEN 10 AND 50 |
3. Managing Data (CRUD)
Aside from reading data, you need to be able to Create, Update, and Delete records.
Insert New Data
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
Update Existing Data
Warning: Always use a
WHEREclause, or you will update every single row in the table!
UPDATE table_name SET column1 = value1 WHERE id = 5;
Delete Data
DELETE FROM table_name WHERE id = 10;
4. Sorting and Grouping
Organize your output to make it readable.
- Sort by name (A-Z):
SELECT * FROM users ORDER BY name ASC; - Sort by price (High to Low):
SELECT * FROM products ORDER BY price DESC; - Group by and Count:
SELECT country, COUNT(*) FROM users GROUP BY country;
5. Joining Tables
SQL’s power lies in connecting related data.
- INNER JOIN: Returns records with matching values in both tables.SQL
SELECT orders.id, customers.name FROM orders INNER JOIN customers ON orders.customer_id = customers.id; - LEFT JOIN: Returns all records from the left table, and matched records from the right.
6. Common Aggregate Functions
Perform calculations on your data sets:
COUNT(): Returns the number of rows.SUM(): Returns the total sum of a numeric column.AVG(): Returns the average value.MAX()/MIN(): Returns the highest or lowest value.
Summary Table: Quick Reference
| Task | Command |
| Search | SELECT column FROM table WHERE condition |
| Add | INSERT INTO table VALUES (...) |
| Change | UPDATE table SET column = value WHERE condition |
| Remove | DELETE FROM table WHERE condition |
| Limit | SELECT * FROM table LIMIT 10 |
Conclusion
Mastering these commands is the first step toward becoming a data pro. Don’t worry about memorizing every syntax detail—even senior developers keep a cheat sheet like this one open on their second monitor!
Leave a Reply