SQL_Cheat_Sheet_with_JOIN_and_Integrity_Constraints

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

SQL Cheat Sheet: Working with Multiple

Tables (Including JOIN and Integrity


Constraints)
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_id INT,
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_id, salary)
VALUES (1, 'John', 'Doe', 1, 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_id = 1;

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_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id;

6. JOINs

JOINs combine rows from two or more tables based on a related column between them.

- INNER JOIN: Returns only matching rows from both tables.


- LEFT JOIN (LEFT OUTER JOIN): Returns all rows from the left table, and matched rows
from the right table.
- RIGHT JOIN (RIGHT OUTER JOIN): Returns all rows from the right table, and matched rows
from the left table.
- FULL JOIN (FULL OUTER JOIN): Returns rows when there is a match in one of the tables.

Syntax:
SELECT column1, column2, ...
FROM table1
JOIN table2 ON table1.column_name = table2.column_name;

Example of INNER JOIN:


SELECT employees.first_name, employees.last_name, departments.department_name,
employees.salary
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;

Example of LEFT JOIN:


SELECT employees.first_name, employees.last_name, departments.department_name,
employees.salary
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;

Example of RIGHT JOIN:


SELECT employees.first_name, employees.last_name, departments.department_name,
employees.salary
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;

Example of FULL JOIN:


SELECT employees.first_name, employees.last_name, departments.department_name,
employees.salary
FROM employees
FULL OUTER JOIN departments ON employees.department_id =
departments.department_id;

7. UNION

Use to combine the result sets of two or more SELECT queries. Removes duplicates.

Syntax:
SELECT column1, column2
FROM table1
UNION
SELECT column1, column2
FROM table2;

Example:
SELECT first_name, last_name FROM employees
UNION
SELECT first_name, last_name FROM customers;

8. Subqueries

A subquery is a query within another query, often used in SELECT, WHERE, or FROM
clauses.

Example of Subquery in WHERE:


SELECT first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE
department_name = 'HR');

Example of Subquery in SELECT:


SELECT first_name, last_name,
(SELECT department_name FROM departments WHERE department_id =
employees.department_id) AS department
FROM employees;

9. Integrity Constraints

Integrity Constraints are used to ensure the accuracy and reliability of data in a database.
- **PRIMARY KEY**: Ensures that each row in a table has a unique identifier. A column with
the primary key constraint cannot contain NULL values.
- **FOREIGN KEY**: Enforces a link between the columns in two tables. It ensures that the
values in one table must exist in another table.
- **NOT NULL**: Ensures that a column cannot have a NULL value.
- **UNIQUE**: Ensures that all values in a column are unique.
- **CHECK**: Ensures that all values in a column satisfy a specific condition.
- **DEFAULT**: Provides a default value for a column when no value is specified.

Syntax for PRIMARY KEY and FOREIGN KEY:


CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
salary DECIMAL(10, 2),
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

Syntax for NOT NULL and UNIQUE:


CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2),
UNIQUE (product_name)
);

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.
- JOIN: Combine data from multiple tables.
- UNION: Combine results from multiple queries.
- CREATE TABLE: Create a new table.
- INSERT INTO: Insert data into a 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy