SQL Notes (2)
SQL Notes (2)
Overview
Structured Query Language (SQL) is a special-purpose programming language designed for managing data held
in a Relational Database Management System (RDBMS). SQL-like languages can also be used in Relational Data
Stream Management Systems (RDSMS), or in "not-only SQL" (NoSQL) databases.
The core DML operations are Create, Read, Update and Delete (CRUD for short) which are performed by the
statements INSERT, SELECT, UPDATE and DELETE. There is also a (recently added) MERGE statement which can
perform all 3 write operations (INSERT, UPDATE, DELETE).
Many SQL databases are implemented as client/server systems; the term "SQL server" describes such a
database. At the same time, Microsoft makes a database that is named "SQL Server". While that database
speaks a dialect of SQL, information specific to that database is not on topic in this tag but belongs into the SQL
Server documentation.
Identifier
This topic is about identifiers, i.e. syntax rules for names of tables, columns, and other database objects. Where
appropriate, the examples should cover variations used by different SQL implementations, or identify the SQL
implementation of the example. Unquoted identifiers can use letters (a-z), digits (0-9), and underscore (_), and
must start with a letter. Depending on SQL implementation, and/or database settings, other characters may be
allowed, some even as the first character, e.g.
Unquoted identifiers are case-insensitive. How this is handled depends greatly on SQL implementation:
MS SQL: Case-preserving, sensitivity defined by database character set, so can be case-sensitive.
MySQL: Case-preserving, sensitivity depends on database setting and underlying file system. Oracle:
Converted to uppercase, then handled like quoted identifier.
PostgreSQL: Converted to lowercase, then handled like quoted identifier.
SQLite: Case-preserving; case insensitivity only for ASCII characters.
Data Types
• INT: Integer numbers.
NULL
NULL in SQL, as well as programming in general, means literally "nothing". In SQL, it is easier to understand as "the
absence of any value". It is important to distinguish it from seemingly empty values, such as the empty string of
which are actually '' or the number 0, neither NULL. It is also important to be careful not to enclose NULL in quotes,
like 'NULL'.
The syntax for filtering for NULL (i.e. the absence of a value) in WHERE blocks is slightly different than filtering for
specific values.
Note that because NULL is not equal to anything, not even to itself, using equality operators = NULL or <> NULL (or !=
NULL) will always yield the truth value of UNKNOWN which will be rejected by WHERE.
SELECT STATEMENT:
The SELECT statement is at the heart of most SQL queries. It defines what result set should be returned by the
query, and is almost always used in conjunction with the FROM clause, which defines what part(s) of the
database should be queried.
Conditions
Conditions can use operators such as:
=: Equal
<>: Not equal (or !=)
>: Greater than
<: Less than
>=: Greater than or equal to
<=: Less than or equal to
BETWEEN: Between a certain range
LIKE: Search for a pattern
IN: Specify multiple possible values for a column
This query selects all columns from the employees table where the age of the employee is greater than
30.
This query selects the first and last names of employees who are older than 30 and work in the HR
department.
This query selects the first name, last name, and salary of employees whose salary is between 50,000
and 100,000.
This query selects the first and last names of employees whose last names start with 'Sm'.
5. Using IN Operator:
This query selects the first and last names of employees who work in the HR, Engineering, or Marketing
departments.
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Example Code
1. Basic ORDER BY:
This query selects the first name, last name, and age of employees, and sorts the results by last
name in ascending order.
This query selects the first name, last name, and department of employees, and sorts the results
first by department in ascending order and then by last name in descending order.
This query selects the first name, last name, and age of employees who are older than 30, and
sorts the results by age in descending order.
This query selects the first name, last name, and salary of employees, sorts the results by salary
in descending order, and limits the output to the top 5 highest salaries.
Example Code
1. Filter and Sort:
This query selects the first name, last name, and department of employees who work in the Engineering
department, and sorts the results by last name in ascending order.
This query selects the first name, last name, and salary of employees who work in the HR department
and earn more than 50,000, sorts the results by salary in descending order, and limits the output to the
top 3 highest salaries.
COUNT()
The COUNT() function returns the number of rows that match a specified condition.
Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
Example Code
SELECT COUNT(employee_id) AS total_employees
FROM employees
WHERE department = 'Engineering';
SUM()
The SUM() function returns the total sum of a numeric column.
Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Example Code
SELECT SUM(salary) AS total_salary
FROM employees
WHERE department = 'Engineering';
This query calculates the total salary of all employees in the Engineering department.
AVG()
The AVG() function returns the average value of a numeric column
Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
Example Code
SELECT AVG(salary) AS average_salary
FROM employees
WHERE department = 'Engineering';
This query calculates the average salary of employees in the Engineering department.
MAX()
The MAX() function returns the maximum value of a specified column.
Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Example Code
SELECT MAX(salary) AS highest_salary
FROM employees
WHERE department = 'Engineering';
This query finds the highest salary among employees in the Engineering department.
MIN()
The MIN() function returns the minimum value of a specified column.
Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
Example Code
SELECT MIN(salary) AS lowest_salary
FROM employees
WHERE department = 'Engineering';
This query finds the lowest salary among employees in the Engineering department.
Syntax
SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1;
Example Code
This query calculates the average salary for each job title within each department.
The HAVING clause is used to filter groups based on conditions. It's similar to the WHERE clause but is
used for groups.
This query calculates the total salary for each department and only includes departments where the
total salary is greater than 500,000.
This query joins the employees and departments tables, then counts the number of employees in each
department.
Example Code
SELECT department, COUNT(employee_id) AS total_employees, AVG(salary) AS average_salary, MAX(salary) AS
highest_salary, MIN(salary) AS lowest_salary
FROM employees
GROUP BY department;
This query provides a summary for each department, including the total number of employees, average salary,
highest salary, and lowest salary.
Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Important Points
The SET clause specifies the columns to be updated and the new values.
The WHERE clause specifies which records should be updated. Omitting the WHERE clause will update
all records in the table.
Example Code
1. Basic UPDATE:
UPDATE employees
SET salary = 60000
WHERE employee_id = 101;
This query updates the salary of the employee with employee_id 101 to 60,000.
UPDATE employees
SET salary = 70000, department = 'HR'
WHERE employee_id = 102;
This query updates the salary to 70,000 and the department to 'HR' for the employee with employee_id
102.
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';
This query increases the salary by 10% for all employees in the Engineering department.
UPDATE employees
SET salary = (SELECT AVG(salary) FROM employees WHERE department = 'Engineering')
WHERE department = 'Marketing';
This query sets the salary of all employees in the Marketing department to the average salary of
employees in the Engineering department.
Syntax
DELETE FROM table_name
WHERE condition;
Important Points
The WHERE clause specifies which records should be deleted. Omitting the WHERE clause will delete all
records in the table.
Always use the WHERE clause to avoid accidentally deleting all records.
Example Code
1. Basic DELETE:
This query deletes the record of the employee with employee_id 103.
This query deletes all employees whose salary is below the average salary of all employees.
Types of Constraints
1. PRIMARY KEY
A PRIMARY KEY constraint uniquely identifies each record in a table. A table can only have one primary key,
which can consist of single or multiple columns (composite primary key).
Syntax
CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
column2 datatype
);
Example Code
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
This example sets the employee_id column as the primary key for the employees table.
2. FOREIGN KEY
A FOREIGN KEY constraint ensures referential integrity by linking the values in one table to the primary key
values in another table. It establishes a relationship between two tables.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
FOREIGN KEY (column1) REFERENCES parent_table(parent_column)
);
Example Code
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);
last_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
This example links the department_id column in the employees table to the department_id column in the
departments table, establishing a foreign key relationship.
3. UNIQUE
A UNIQUE constraint ensures that all values in a column are unique. Unlike the primary key, multiple unique
constraints can be applied to a table, and they can contain NULL values unless explicitly defined as NOT NULL.
Syntax
CREATE TABLE table_name (
column1 datatype UNIQUE,
column2 datatype
);
Example Code
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
This example sets the email column in the employees table to have unique values.
4. NOT NULL
A NOT NULL constraint ensures that a column cannot have a NULL value. This is used to enforce mandatory
data.
Syntax
CREATE TABLE table_name (
column1 datatype NOT NULL,
column2 datatype
);
Example Code
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL
);
This example ensures that the first_name and last_name columns in the employees table cannot have NULL
values.
5. CHECK
A CHECK constraint ensures that all values in a column satisfy a specific condition.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
CHECK (condition)
);
Example Code
This example ensures that the age column in the employees table has values greater than or equal to 18.
Constraints can also be added to existing tables using the ALTER TABLE statement.
Syntax
ALTER TABLE table_name
ADD CONSTRAINT constraint_name constraint_type (column_name);
Example Code
Dropping Constraints
Constraints can be dropped if they are no longer needed.
Syntax
ALTER TABLE table_name
DROP CONSTRAINT constraint_name;
Example Code
1. Dropping a UNIQUE Constraint:
1. Uniqueness:
o Primary Key: Ensures that all values in the primary key column(s) are unique and not NULL.
Each table can have only one primary key.
o Unique Key: Ensures that all values in the unique key column(s) are unique, but they can have
NULL values. A table can have multiple unique keys.4
2. Nullability:
o Primary Key: Cannot contain NULL values.
o Unique Key: Can contain NULL values, but each NULL value must be unique (if the column allows
NULLs).
3. Indexing:
o Primary Key: Automatically creates a clustered index on the primary key column(s). If a
clustered index already exists, a non-clustered index is created.
o Unique Key: Automatically creates a non-clustered index on the unique key column(s) to enforce
uniqueness.
4. Purpose:
o Primary Key: Primarily used to uniquely identify each row in a table. It is the main identifier for
table records.
o Unique Key: Used to enforce uniqueness on one or more columns that are not the primary
identifier for the table but still need to ensure uniqueness.
6. Combination of Columns:
o Primary Key: Can be a single column or a combination of columns (composite primary key) that
uniquely identify a record.
o Unique Key: Can also be a single column or a combination of columns (composite unique key)
that enforce uniqueness.
A transaction in SQL is a sequence of one or more SQL operations treated as a single unit of work. Transactions
ensure that a series of operations are completed successfully and consistently, or none of them are applied if
an error occurs. This is crucial for maintaining data integrity and consistency in a database.
ACID Properties
1. Atomicity: Ensures that all operations within a transaction are completed; if one operation fails, the
entire transaction fails, and the database state is left unchanged.
2. Consistency: Ensures that a transaction takes the database from one valid state to another valid state,
maintaining database invariants.
3. Isolation: Ensures that concurrently executed transactions do not interfere with each other.
4. Durability: Ensures that once a transaction has been committed, it remains so, even in the event of a
system failure.
Starting a Transaction
Transactions are started with the BEGIN TRANSACTION or START TRANSACTION statement.
Syntax
BEGIN TRANSACTION;
-- or
START TRANSACTION;
Committing a Transaction
To save the changes made by the transaction permanently, use the COMMIT statement.
Syntax
COMMIT;
Syntax
ROLLBACK;
Example Code
Basic Transaction:
BEGIN TRANSACTION;
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';
COMMIT;
This transaction increases the salary of employees in the Engineering department by 10% and deletes the
employee with employee_id 104. If both operations succeed, the changes are committed.
BEGIN TRANSACTION;
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';
-- Simulating an error
IF (SELECT COUNT(*) FROM employees WHERE employee_id = 104) > 0
BEGIN
ROLLBACK;
END
ELSE
BEGIN
COMMIT;
END;
This transaction increases the salary of employees in the Engineering department by 10% and attempts to delete
the employee with employee_id 104. If the employee with employee_id 104 still exists after the delete operation,
the transaction is rolled back.
Savepoints
Savepoints allow you to set a point within a transaction to which you can later roll back. This is useful for
partially rolling back a transaction.
Syntax
SAVEPOINT savepoint_name;
ROLLBACK TO savepoint_name;
Example Code:
BEGIN TRANSACTION;
UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';
SAVEPOINT before_delete;
-- Simulating an error
IF (SELECT COUNT(*) FROM employees WHERE employee_id = 104) > 0
BEGIN
ROLLBACK TO before_delete;
COMMIT;
END
ELSE
BEGIN
COMMIT;
END;
In this example, if the delete operation fails, the transaction rolls back to the savepoint before_delete and then
commits the remaining changes.
Isolation levels control the visibility of changes made by concurrent transactions. The higher the isolation level,
the more isolated the transactions are from each other, but this may impact performance.
Kedar Joshi, NIrmaan Org. Page 19
SQL Notes – Microsoft FRYSP
1. READ UNCOMMITTED: Lowest isolation level. Transactions can see uncommitted changes from other
transactions, which may lead to dirty reads.
2. READ COMMITTED: Default isolation level. Transactions cannot see uncommitted changes from other
transactions.
3. REPEATABLE READ: Ensures that if a transaction reads the same data multiple times, it will see the
same values each time, preventing non-repeatable reads.
4. SERIALIZABLE: Highest isolation level. Transactions are completely isolated from each other, ensuring
serializable transactions but potentially impacting performance.
Syntax
In a relational database, tables are often related to each other through keys, which establish the relationships
between different pieces of data. These relationships allow you to combine data from multiple tables in
meaningful ways.
Types of Relationships
1. One-to-One: Each row in Table A is linked to one and only one row in Table B, and vice versa.
2. One-to-Many: Each row in Table A can be linked to multiple rows in Table B, but each row in Table B is
linked to only one row in Table A.
3. Many-to-Many: Each row in Table A can be linked to multiple rows in Table B, and each row in Table B
can be linked to multiple rows in Table A. This typically requires a junction table.
o Example: Students and courses (students can enroll in multiple courses, and courses can have
multiple students).
Establishing Relationships
Foreign Key: A column or set of columns in one table that refers to the primary key in another table.
An INNER JOIN is used to combine rows from two or more tables based on a related column between them. It
returns only the rows where there is a match in both tables.
Syntax
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
Example Tables
Let's consider two tables: employees and departments.
LEFT JOIN
A LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table, and the matched rows from the right
table. If there is no match, the result is NULL on the side of the right table.
Syntax
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
Example Code
This query retrieves all employees and their department names. If an employee does not belong to any
department, the department name will be NULL.
RIGHT JOIN
A RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table, and the matched rows from the left
table. If there is no match, the result is NULL on the side of the left table.
Syntax
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Example Code
This query retrieves all departments and their employees. If a department does not have any employees, the
employee details will be NULL.
Syntax
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
Example Code
SELECT employees.first_name, employees.last_name, departments.department_name
FROM employees
FULL OUTER JOIN departments
ON employees.department_id = departments.department_id;
This query retrieves all employees and all departments. If an employee does not belong to a department, the
department details will be NULL, and if a department does not have any employees, the employee details will be
NULL.
Writing Subqueries
Subqueries, also known as inner queries or nested queries, are queries nested inside another SQL query. They
are used to perform operations that need to be executed before the main query.
Types of Subqueries
1. Single-Row Subquery: Returns a single row.
2. Multi-Row Subquery: Returns multiple rows.
3. Scalar Subquery: Returns a single value.
4. Correlated Subquery: References columns from the outer query.
Syntax
SELECT column1, column2
FROM table1
WHERE column3 = (SELECT column1 FROM table2 WHERE condition);
Example Code
1. Single-Row Subquery:
SELECT first_name, last_name
FROM employees
WHERE department_id = (SELECT department_id FROM departments WHERE department_name = 'HR');
This query retrieves the names of employees who work in the HR department.
2. Multi-Row Subquery:
This query retrieves the names of employees who work in departments located in New York.
3. Scalar Subquery:
This query retrieves the names of employees along with their department names.
4. Correlated Subquery:
This query retrieves the names and salaries of employees whose salaries are above the average salary
of their respective departments.
Views
A view in SQL is a virtual table based on the result-set of an SQL statement. Views contain rows and columns
just like a real table, and the fields in a view are fields from one or more real tables in the database.
Advantages of Views
1. Simplify Complex Queries: Views can simplify complex queries by breaking them into smaller, more
manageable parts.
2. Data Security: Views can restrict access to certain data by only exposing specific columns or rows.
3. Data Abstraction: Views provide a level of abstraction by hiding the complexity of the database schema.
4. Reusability: Views can be reused by different users and queries.
Creating a View
Example
CREATE VIEW employee_details AS
SELECT e.employee_id, e.first_name, e.last_name, d.department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;
This example creates a view employee_details that combines data from the employees and departments tables.
Querying a View
Example
Syntax
Example
This updates the employee_details view to include the location column from the departments table.
Dropping a View
Example