SQL Tutorial: SQL Everything in 1 Page

1. General Shortcuts

  • SELECT * FROM table; → Selects all columns
  • SELECT column1, column2 FROM table; → Selects specific columns
  • SELECT DISTINCT column FROM table; → Returns unique values

2. Filtering Data

  • SELECT * FROM table WHERE column = ‘value’; → Filters rows
  • SELECT * FROM table WHERE column LIKE ‘%value%’; → Finds partial matches
  • SELECT * FROM table WHERE column BETWEEN value1 AND value2; → Finds range
  • SELECT * FROM table WHERE column IN (‘value1’, ‘value2’); → Matches multiple values

3. Sorting and Limiting

  • SELECT * FROM table ORDER BY column ASC; → Sorts ascending
  • SELECT * FROM table ORDER BY column DESC; → Sorts descending
  • SELECT * FROM table LIMIT 10; → Limits to 10 rows

4. Aggregation & Grouping

  • SELECT COUNT(*) FROM table; → Counts rows
  • SELECT SUM(column) FROM table; → Sum of column values
  • SELECT AVG(column) FROM table; → Average of values
  • SELECT column, COUNT(*) FROM table GROUP BY column; → Groups by column

5. Joins

  • SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id;
  • SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id;
  • SELECT * FROM table1 RIGHT JOIN table2 ON table1.id = table2.id;

6. Subqueries

  • SELECT column FROM table WHERE column = (SELECT MAX(column) FROM table);

7. Modifying Data

  • INSERT INTO table (col1, col2) VALUES (‘val1’, ‘val2’);
  • UPDATE table SET column = ‘new_value’ WHERE condition;
  • DELETE FROM table WHERE condition;

8. Creating & Altering Tables

  • CREATE TABLE table (id INT PRIMARY KEY, name VARCHAR(100));
  • ALTER TABLE table ADD column_name DATA_TYPE;
  • DROP TABLE table;

Leave a Reply

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