SQL (Structured Query Language)
SQL is the standard language for managing and manipulating relational databases.
Core Concepts:
• DDL (Data Definition Language): Commands that define the database structure.
o CREATE: Creates a new table, view, or other database object.
o ALTER: Modifies an existing database object (e.g., adds a column to a table).
o DROP: Deletes an entire table, view, or other object.
o TRUNCATE: Removes all records from a table but keeps the table structure.
• DML (Data Manipulation Language): Commands that manage data within tables.
o SELECT: Retrieves data from one or more tables.
o INSERT: Adds new rows of data to a table.
o UPDATE: Modifies existing data in a table.
o DELETE: Removes rows from a table.
• DCL (Data Control Language): Commands that control access to data.
o GRANT: Gives a user permission to perform specific actions.
o REVOKE: Removes a user's permissions.
• Joins: Combine rows from two or more tables based on a related column.
o INNER JOIN: Returns records that have matching values in both tables.
o LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table. The result is NULL on the right side if there is no match.
o RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table.
o FULL (OUTER) JOIN: Returns all records when there is a match in either the left or
right table.
o SELF JOIN: A regular join, but the table is joined with itself.
o CROSS JOIN: Returns the Cartesian product of the two tables (every row from the
first table combined with every row from the second).
• Filtering & Sorting:
o WHERE: Filters records before any groupings are made.
o GROUP BY: Groups rows that have the same values into summary rows (e.g., "find
the number of employees in each department").
o HAVING: Filters records after the GROUP BY clause. It is used on aggregated data
(e.g., "find departments with more than 5 employees").
o ORDER BY: Sorts the result-set in ascending (ASC) or descending (DESC) order.
• Keys:
o Primary Key: A unique identifier for each row in a table (cannot be NULL).
o Foreign Key: A field in one table that uniquely identifies a row in another table. It
creates a link between the tables.
o Unique Key: Ensures all values in a column are unique (can contain NULL).
o Composite Key: A primary key made up of two or more columns.
• Functions:
o Aggregate Functions: Operate on a set of values and return a single value.
▪ COUNT(): Returns the number of rows.
▪ SUM(): Returns the sum of a numeric column.
▪ AVG(): Returns the average value.
▪ MAX()/MIN(): Returns the maximum/minimum value.
1. CREATE TABLE
The `CREATE TABLE` statement is used to create a new table in the database.
General Syntax:
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
...
PRIMARY KEY (column_name),
FOREIGN KEY (column_name) REFERENCES other_table(column_name)
);
Example Query:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
DepartmentID INT,
HireDate DATE DEFAULT GETDATE(),
FOREIGN KEY (DepartmentID) REFERENCES Departments(DepartmentID)
);
This creates an `Employees` table with various columns, sets `EmployeeID` as the Primary Key, and
defines a Foreign Key relationship to a `Departments` table.
2. ALTER TABLE
The `ALTER TABLE` statement is used to add, delete, or modify columns in an existing table. It can
also be used to add and drop various constraints.
General Syntax (Adding a Column):
ALTER TABLE table_name
ADD column_name datatype;
Example Query (Add Column):
ALTER TABLE Employees
ADD Email VARCHAR(100);
This adds a new column named `Email` to the `Employees` table.
General Syntax (Modifying a Column):
Note: Syntax for modifying a column varies by database (e.g., `ALTER COLUMN` in SQL Server,
`MODIFY` in MySQL/Oracle).
-- For SQL Server / MS Access
ALTER TABLE table_name
ALTER COLUMN column_name new_datatype;
-- For MySQL / Oracle (Pre-10G)
ALTER TABLE table_name
MODIFY column_name new_datatype;
Example Query (Modify Column):
-- Changing the data type from VARCHAR(100) to VARCHAR(255)
ALTER TABLE Employees
ALTER COLUMN Email VARCHAR(255);
General Syntax (Dropping a Column):
ALTER TABLE table_name
DROP COLUMN column_name;
Example Query (Drop Column):
ALTER TABLE Employees
DROP COLUMN Email;
This removes the `Email` column from the `Employees` table.
3. DROP TABLE
The `DROP TABLE` statement is used to delete an existing table completely, including its structure,
data, indexes, and privileges.
General Syntax:
DROP TABLE table_name;
Example Query:
DROP TABLE TemporaryData;
Warning: This will permanently delete the `TemporaryData` table and all its information. Use
withextreme caution.
4. TRUNCATE TABLE
The `TRUNCATE TABLE` statement is used to delete all the data inside a table, but the table structure
(columns, constraints, etc.) remains.
General Syntax:
TRUNCATE TABLE table_name;
Example Query:
TRUNCATE TABLE AuditLog;
This quickly removes all records from the `AuditLog` table but keeps the table empty and ready for
new data. It's faster than `DELETE FROM table_name;` for large tables.
Key Difference: DROP vs. TRUNCATE vs. DELETE
Removes Table Can be Rolled
Command Removes Data? Speed
Structure? Back?
Only if in a
DROP Yes Yes Fastest
transaction
Removes Table Can be Rolled
Command Removes Data? Speed
Structure? Back?
Only if in a
TRUNCATE Yes No Faster
transaction
Yes (can be
DELETE No Yes Slowest
filtered)
1. SELECT
The `SELECT` statement is used to retrieve data from one or more tables.
General Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column_name [ASC|DESC]
GROUP BY column_name
HAVING condition;
Example Queries:
1. Select all columns from a table:
SELECT FROM Employees;
2. Select specific columns with a filter:
SELECT FirstName, LastName, Salary
FROM Employees
WHERE Department = 'IT';
3. Select with sorting and aggregation:
SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 50000
ORDER BY AvgSalary DESC;
2. INSERT
The `INSERT` statement is used to add new rows of data to a table.
General Syntax:
There are two main ways:
1. Specifying both columns and values:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
2. Only specifying values (must provide values for all columns in order):
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
Example Queries:
1. Insert into specific columns:
INSERT INTO Employees (FirstName, LastName, Department, HireDate)
VALUES ('Jane', 'Smith', 'Sales', '2023-10-26');
2. Insert into all columns (less safe):
INSERT INTO Employees
VALUES (105, 'Mike', 'Jones', 'HR', 55000, '2023-11-01');
This requires knowing the exact order of all columns in the table.
3. UPDATE
The `UPDATE` statement is used to modify existing records in a table. Always use a `WHERE` clause
unless you intend to update every row in the table.
General Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example Queries:
1. Update a specific record:
UPDATE Employees
SET Salary = 75000
WHERE EmployeeID = 101;
Updates the salary for the employee with ID 101.
2. Update multiple columns for a group of records:
UPDATE Employees
SET Department = 'Marketing', Salary = Salary 1.10
WHERE Department = 'Sales';
Moves all Sales employees to the Marketing department and gives them a 10% raise.
Warning: Without a `WHERE` clause, the update applies to all rows:
-- DANGER: Updates EVERY row in the table.
UPDATE Employees
SET Status = 'Inactive';
4. DELETE
The `DELETE` statement is used to remove existing records from a table. Always use a `WHERE`
clause unless you intend to empty the entire table.
General Syntax:
DELETE FROM table_name
WHERE condition;
Example Queries:
1. Delete a specific record:
DELETE FROM Employees
WHERE EmployeeID = 105;
Deletes the employee with ID 105.
2. Delete a group of records:
DELETE FROM Logs
WHERE created_date < '2023-01-01';
Deletes all log entries from before 2023.
Warning: Without a `WHERE` clause, the command deletes all rows:
-- DANGER: Deletes EVERY row in the table. The table will be empty.
DELETE FROM Employees;
To delete all rows more efficiently, consider `TRUNCATE TABLE table_name;`.
DCL (Data Control Language)
DCL commands are used to control access to the data in the database. They are crucial for database
security.
1. GRANT
Gives specific privileges to a user or role.
General Syntax:
GRANT privilege1, privilege2, ...
ON database_object
TO user_or_role;
Common Privileges:
`SELECT`: Allows reading data.
`INSERT`: Allows inserting data.
`UPDATE`: Allows modifying data.
`DELETE`: Allows deleting data.
`ALL`: Grants all permissions.
Example Queries:
1. Grant read access on a table to a user:
GRANT SELECT
ON Employees
TO user_jane;
Allows `user_jane` to run `SELECT` queries on the `Employees` table.
2. Grant multiple permissions on a table to a role:
GRANT SELECT, INSERT, UPDATE
ON ServiceRequests
TO role_support_agent;
Allows anyone in the `role_support_agent` role to read, create, and update records in the
`ServiceRequests` table.
2. REVOKE
Takes away previously granted privileges from a user or role.
General Syntax:
REVOKE privilege1, privilege2, ...
ON database_object
FROM user_or_role;
Example Queries:
1. Revoke update permission from a user:
REVOKE UPDATE
ON Employees
FROM user_jane;
`user_jane` can no longer update the `Employees` table.
2. Revoke all permissions on a table:
REVOKE ALL
ON ServiceRequests
FROM role_support_agent;
Removes all privileges that the `role_support_agent` had on the `ServiceRequests` table.
JOINS
JOINS are used to combine rows from two or more tables based on a related column between them.
For the examples, assume we have two tables:
`Employees` with columns: `EmployeeID`, `FirstName`, `LastName`, `DepartmentID`
`Departments` with columns: `DepartmentID`, `DepartmentName`
1. INNER JOIN
Returns only the records that have matching values in both tables.
General Syntax:
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example Query:
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
INNER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
This returns only employees who are assigned to a department that exists in the `Departments`
table.
2. LEFT (OUTER) JOIN
Returns all records from the left table (`Employees`), and the matched records from the right table
(`Departments`). The result is `NULL` on the right side if there is no match.
General Syntax:
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example Query:
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
LEFT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
This returns all employees, and if they don't have a valid department ID, the `DepartmentName` will
be `NULL`. Useful for finding employees not assigned to any department.
3. RIGHT (OUTER) JOIN
Returns all records from the right table (`Departments`), and the matched records from the left table
(`Employees`). The result is `NULL` on the left side if there is no match.
General Syntax:
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example Query:
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
RIGHT JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
This returns all departments, and if a department has no employees, the employee name columns
will be `NULL`. Useful for finding empty departments.
4. FULL (OUTER) JOIN
Returns all records when there is a match in either the left or right table. It combines the results of
both LEFT and RIGHT joins.
General Syntax:
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
Example Query:
SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
FROM Employees
FULL OUTER JOIN Departments
ON Employees.DepartmentID = Departments.DepartmentID;
This returns all employees and all departments. If an employee has no department, the department
columns are `NULL`. If a department has no employees, the employee columns are `NULL`.
5. SELF JOIN
A regular join where a table is joined with itself. It's useful for querying hierarchical data or
comparing rows within the same table.
General Syntax:
SELECT A.column, B.column
FROM table_name A, table_name B
WHERE A.common_column = B.common_column;
-- Or using standard JOIN syntax:
SELECT A.column, B.column
FROM table_name A
INNER JOIN table_name B
ON A.common_column = B.common_column;
Example Query:
Find all employees and the name of their manager (assuming managers are also in the `Employees`
table with a `ReportsTo` column pointing to their manager's `EmployeeID`).
SELECT E1.FirstName AS EmployeeName, E2.FirstName AS ManagerName
FROM Employees E1
LEFT JOIN Employees E2 ON E1.ReportsTo = E2.EmployeeID;
6. CROSS JOIN
Produces the Cartesian product of the two tables, meaning it joins every row of the first table with
every row of the second table.
General Syntax:
SELECT columns
FROM table1
CROSS JOIN table2;
Example Query:
SELECT Employees.FirstName, Shirts.Size
FROM Employees
CROSS JOIN Shirts;
This would return a list of every possible combination of employee and shirt size. (e.g., if there are 10
employees and 3 shirt sizes, the result will have 30 rows).
The Complete Syntax & Execution Order
The general syntax for a `SELECT` statement, incorporating all these clauses, is as follows. The order
in which you write them is critical:
SELECT column_list
FROM table_name
WHERE condition
GROUP BY column_list
HAVING condition
ORDER BY column_list [ASC | DESC];
Execution Order (Priority) - HOW SQL PROCESSES THE QUERY
SQL does not process the clauses in the order you write them. The execution order (or logical
processing order) determines the priority of filtering and operations:
1. FROM: Identifies the source tables.
2. WHERE: Filters individual rows from the source tables.
3. GROUP BY: Groups the filtered rows into summary rows.
4. HAVING: Filters the groups created by `GROUP BY`.
5. SELECT: Processes the final column list and calculates any expressions or aggregates.
6. ORDER BY: Sorts the final result set.
Visual Guide to Priority:
`FROM` -> `WHERE` (filters rows) -> `GROUP BY` (creates groups) -> `HAVING` (filters groups) ->
`SELECT` (shapes data) -> `ORDER BY` (sorts output)
Detailed Breakdown with Examples
Let's use an `Employees` table:
EmployeeID FirstName LastName Department Salary
101 Jane Doe IT 75000
102 Mike Smith Sales 55000
EmployeeID FirstName LastName Department Salary
103 Alice Brown IT 80000
104 Tom Jones HR 60000
105 Sarah Davis Sales 58000
106 Bob Wilson IT 90000
107 Emily Clark Sales 56000
1. WHERE
Purpose: Filters raw, individual rows before any aggregation happens. It cannot be used with
aggregate functions (`COUNT`, `SUM`, `AVG`).
Analogy: "First, only look at employees who work in the IT department."
Example:
SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
WHERE Department = 'IT' -- FILTER ROWS first, then aggregate
GROUP BY Department;
Result: Only IT rows are considered for the average calculation.
Department AvgSalary
IT 81666.66
2. GROUP BY
Purpose: Groups the rows that remain after the `WHERE` clause into summary buckets based on
identical values in the specified columns.
Analogy: "Now, group the remaining employees by their department."
Example:
SELECT Department, COUNT() AS EmployeeCount
FROM Employees
GROUP BY Department; -- Group rows by department value
Result:
Department EmployeeCount
IT 3
Sales 3
HR 1
3. HAVING
Purpose: Filters the groups created by `GROUP BY`. Because it works on groups, it can use
aggregate functions.
Analogy: "Now, from those department groups, only show me the ones that have more than 2
employees."
Example:
SELECT Department, COUNT() AS EmployeeCount
FROM Employees
GROUP BY Department
HAVING COUNT() > 2; -- FILTER GROUPS after aggregation
Result: The HR group (count=1) is filtered out.
Department EmployeeCount
IT 3
Sales 3
4. ORDER BY
Purpose: Sorts the final result set. It is always the last clause to be processed and can use column
aliases defined in the `SELECT` clause (like `AvgSalary`).
Analogy: "Finally, sort the final list from highest to lowest average salary."
Example:
SELECT Department, AVG(Salary) AS AvgSalary
FROM Employees
GROUP BY Department
HAVING AVG(Salary) > 60000
ORDER BY AvgSalary DESC; -- Sort the final result
Result:
Department AvgSalary
IT 81666.66
HR 60000.00
Master Example: Combining All Clauses
Query Goal: "Find all departments (excluding HR) that have an average salary greater than \$60,000,
and display them sorted from highest to lowest average salary."
SELECT
Department,
COUNT() AS NumEmployees, -- Aggregate in SELECT is OK now
AVG(Salary) AS AvgSalary -- because groups are formed.
FROM Employees
WHERE Department <> 'HR' -- 1. Filter out HR rows first
GROUP BY Department -- 2. Group the remaining rows
HAVING AVG(Salary) > 60000 -- 3. Filter the created groups
ORDER BY AvgSalary DESC; -- 4. Sort the final result
Step-by-step execution:
1. FROM Employees: Get the entire table.
2. WHERE Department <> 'HR': Remove the row for Tom Jones in HR.
3. GROUP BY Department: Form groups for IT and Sales.
4. HAVING AVG(Salary) > 60000: Check the average salary of each group. Sales (~56333) is less than
60000 and is filtered out. Only the IT group remains.
5. SELECT ...: Calculate the final `COUNT()` and `AVG(Salary)` for the IT group.
6. ORDER BY AvgSalary DESC: Sort the single result. (Not much to sort here, but the clause is
executed).
Final Result:
Department NumEmployees AvgSalary
IT 3 81666.66