SQL Cheat Sheet: The Essential Commands Every Developer Needs

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.

OperatorDescriptionExample
=Equal toWHERE age = 25
!= or <>Not equal toWHERE status != 'active'
LIKEPattern matchingWHERE name LIKE 'J%' (Starts with J)
INMultiple possible valuesWHERE country IN ('USA', 'UK')
BETWEENWithin a rangeWHERE 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 WHERE clause, 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.SQLSELECT 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

TaskCommand
SearchSELECT column FROM table WHERE condition
AddINSERT INTO table VALUES (...)
ChangeUPDATE table SET column = value WHERE condition
RemoveDELETE FROM table WHERE condition
LimitSELECT * 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

Your email address will not be published. Required fields are marked *