0% found this document useful (0 votes)
4 views

SQL_Cheat_Sheet

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL_Cheat_Sheet

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Cheat Sheet: From Creating Tables

to ORDER BY
1. CREATE TABLE

Use to create a new table in the database.

Syntax:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
...
);

Example:
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);

2. INSERT INTO

Use to insert data into a table.

Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Example:
INSERT INTO employees (employee_id, first_name, last_name, department, salary)
VALUES (1, 'John', 'Doe', 'Sales', 50000.00);
3. SELECT

Use to query data from a table.

Syntax:
SELECT column1, column2, ...
FROM table_name;

Example:
SELECT first_name, last_name, salary
FROM employees;

4. WHERE Clause

Use to filter records based on a condition.

Syntax:
SELECT column1, column2
FROM table_name
WHERE condition;

Example:
SELECT first_name, last_name, salary
FROM employees
WHERE department = 'Sales';

5. Aggregation Functions

Use to calculate aggregate values (e.g., SUM, COUNT, AVG).

- SUM(): Adds up values in a column.


- COUNT(): Counts the number of rows.
- AVG(): Calculates the average of a column.
- MIN(): Finds the minimum value.
- MAX(): Finds the maximum value.

Example:
SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

6. GROUP BY

Used to group rows that have the same values in specified columns.

Syntax:
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;

Example:
SELECT department, COUNT(employee_id) AS total_employees
FROM employees
GROUP BY department;

7. HAVING Clause

Used to filter groups after the GROUP BY operation.

Syntax:
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1
HAVING condition;

Example:
SELECT department, COUNT(employee_id) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(employee_id) > 1;

8. ORDER BY

Used to sort the result set in ascending or descending order.

Syntax:
SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];

Example:
SELECT first_name, last_name, salary
FROM employees
ORDER BY salary DESC;

9. WHERE vs. HAVING

- WHERE filters rows before grouping (applies to individual rows).


- HAVING filters after grouping (applies to groups).

10. Combining Clauses

You can combine WHERE, GROUP BY, HAVING, and ORDER BY to create complex queries.

Example:
SELECT department, SUM(salary) AS total_salary
FROM employees
WHERE salary > 50000
GROUP BY department
HAVING SUM(salary) > 100000
ORDER BY total_salary DESC;

Quick Reference

- SELECT: Retrieve data from a table.


- WHERE: Filter rows before aggregation.
- GROUP BY: Group rows based on column values.
- HAVING: Filter groups after aggregation.
- ORDER BY: Sort results.
- INSERT INTO: Insert data into a table.
- CREATE TABLE: Create a new table.
Common Data Types

- INT: Integer numbers.


- VARCHAR(length): Variable-length character strings.
- DECIMAL(m, d): Decimal numbers with a specified precision and scale.
- DATE: Date values.
- BOOLEAN: True/False values.

You might also like