SQL Comprehensive Notes
SQL Comprehensive Notes
Table of Contents
1. Introduction to SQL
Overview of SQL
Characteristics of SQL
Advantages of SQL
SQL Data Types and Literals
2. Types of SQL Commands
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Control Language (DCL)
Transaction Control Language (TCL)
Data Query Language (DQL)
3. Basic SQL Queries
SELECT Statement
Filtering with WHERE Clause
Sorting with ORDER BY
Removing Duplicates with DISTINCT
4. Logical Operators
BETWEEN Operator
IN Operator
AND Operator
OR Operator
NOT Operator
5. NULL Values
Understanding NULL Values
Disallowing NULL Values
Comparisons Using NULL Values
IS NULL and IS NOT NULL Operators
6. Integrity Constraints
Primary Key Constraints
NOT NULL Constraints
UNIQUE Constraints
CHECK Constraints
Referential Integrity (Foreign Key)
7. Nested Queries
Introduction to Nested Queries
Types of Subqueries
Correlated Nested Queries
Set-Comparison Operators
EXISTS and NOT EXISTS
8. Aggregate Functions
Common Aggregate Functions
The GROUP BY Clause
The HAVING Clause
Combining GROUP BY and HAVING
9. Joins
Inner Joins
Outer Joins
Left Outer Joins
Right Outer Joins
Full Outer Joins
Self Joins
10. Other SQL Objects
Views
Sequences
Indexes
Triggers
Stored Procedures
11. Advanced SQL Concepts
Window Functions
Common Table Expressions (CTEs)
Transactions
Database Security
Introduction to SQL
Overview of SQL
SQL (Structured Query Language) is a specialized programming language designed for managing data in relational database management systems
(RDBMS). Developed in the early 1970s at IBM by Donald D. Chamberlin and Raymond F. Boyce, SQL has evolved to become the standard language
for relational database management.
The SQL language is divided into several components, including data definition, data manipulation, data control, and data query elements. Through
these components, SQL provides a comprehensive set of tools for managing relational databases.
Oracle Database
Microsoft SQL Server
MySQL
PostgreSQL
SQLite
IBM Db2
MariaDB
While each database system may have its own proprietary extensions to SQL, the core language remains largely consistent across platforms, making
SQL knowledge transferable between different database environments.
Characteristics of SQL
SQL possesses several key characteristics that make it a powerful and widely used language for database management:
1. Declarative Nature: SQL is primarily a declarative language, meaning that users specify what results they want rather than how to obtain them.
The database system determines the most efficient way to execute queries.
2. Non-Procedural: Unlike procedural programming languages that require step-by-step instructions, SQL statements describe the data to be
retrieved or manipulated without specifying the exact process.
3. Set-Based Operations: SQL operates on sets of data rather than processing records one at a time. This set-based approach allows for
efficient data processing.
4. English-Like Syntax: SQL uses commands and clauses that resemble English language phrases, making it relatively easy for non-
programmers to learn and understand.
5. ANSI Standard: SQL is standardized by both the American National Standards Institute (ANSI) and the International Organization for
Standardization (ISO), ensuring a level of consistency across different implementations.
6. Data Independence: SQL separates the logical structure of a database from its physical implementation, providing data independence.
7. Client-Server Architecture: SQL typically operates in a client-server model, where clients send SQL requests to a database server, which
processes the requests and returns results.
8. Integrated Data Definition: SQL includes capabilities for both data definition (creating and modifying database structures) and data
manipulation (inserting, updating, and querying data).
9. Support for Integrity Constraints: SQL allows for the definition of various integrity constraints to ensure data accuracy and consistency.
10. Transaction Support: SQL includes commands for managing transactions, allowing for the grouping of operations that must be executed
together.
Advantages of SQL
SQL offers numerous advantages that have contributed to its widespread adoption:
1. Standardization: As an ANSI/ISO standard language, SQL provides consistency across different database platforms, reducing the learning
curve when switching between systems.
2. Simplicity and Readability: The English-like syntax of SQL makes it relatively easy to learn, read, and write compared to many programming
languages.
3. Powerful Data Manipulation: SQL provides robust capabilities for querying, filtering, sorting, and aggregating data, allowing users to extract
precisely the information they need.
4. Data Independence: SQL abstracts the physical storage details from users, allowing them to work with logical data structures without needing
to understand the underlying storage mechanisms.
5. Reduced Data Redundancy: Through normalization and relational design, SQL databases can minimize data duplication, leading to more
efficient storage and reduced inconsistencies.
6. Data Integrity: SQL supports various constraints that help maintain data accuracy and consistency, including primary keys, foreign keys, and
check constraints.
7. Concurrent Access: SQL databases are designed to handle multiple users accessing data simultaneously, with built-in mechanisms to prevent
conflicts.
8. Security: SQL includes features for controlling access to data through permissions and privileges, allowing administrators to restrict who can
view or modify specific data.
9. Scalability: Modern SQL implementations can scale to handle massive datasets and high transaction volumes.
10. Integration Capabilities: SQL databases can be easily integrated with various programming languages and applications, making them
versatile for different use cases.
11. Transaction Processing: SQL supports ACID (Atomicity, Consistency, Isolation, Durability) properties for reliable transaction processing.
12. Improved Data Organization: The relational model underlying SQL facilitates logical organization of data into tables with defined relationships.
Effect: Creates a table with two columns that can store integer values:
ProductID Quantity
NULL NULL
ProductID Quantity
1 100
2 250
2. SMALLINT: Similar to INTEGER but uses less storage space and has a smaller range.
Effect: Creates a table with columns that can store small integers:
SettingID Value
NULL NULL
SettingID Value
1 5
2 10
3. DECIMAL(p,s) / NUMERIC(p,s): Fixed-point numbers where 'p' is precision (total digits) and 's' is scale (digits after decimal).
CREATE TABLE Finances (
TransactionID INT,
Amount DECIMAL(10,2) -- 10 digits in total, 2 after decimal point
);
Effect: Creates a table where the Amount column can store exact decimal values:
TransactionID Amount
NULL NULL
TransactionID Amount
1 125.45
2 9999.99
3 1234567.89
StateCode CountryCode
NULL NULL
StateCode CountryCode
'CA' 'USA'
'NY' 'USA'
Note: 'CA' is stored as 'CA ' (with a trailing space) internally since it's shorter than the defined length, but the trailing space is typically trimmed
when displaying the data.
2. VARCHAR(n) / CHARACTER VARYING(n): Variable-length character string with maximum length 'n'.
INSERT INTO Customers (CustomerID, Name, Address) VALUES (1, 'John Smith', '123 Main St, Anytown, USA');
INSERT INTO Customers (CustomerID, Name, Address) VALUES (2, 'Jane Doe', '456 Oak Ave');
EventID EventDate
NULL NULL
EventID EventDate
1 2023-06-15
2 2023-12-25
LogID CreatedAt
NULL NULL
LogID CreatedAt
LogID CreatedAt
1 2023-06-15 14:30:00
2 2023-06-15 15:45:12
INSERT INTO UserPreferences (UserID, ReceiveEmails, DarkModeEnabled) VALUES (1, TRUE, FALSE);
INSERT INTO UserPreferences (UserID, ReceiveEmails, DarkModeEnabled) VALUES (2, FALSE, TRUE);
SQL Literals
Literals are fixed values used in SQL statements. They represent constant values of different data types:
3. Date Literals: Format varies by DBMS but often uses quotes with specific formats.
CREATE
The CREATE command is used to create new database objects such as tables, views, indexes, and stored procedures.
Creating a Database:
Creating a Table:
Creating a View:
Effect: Creates a virtual table that presents selected columns from the Employees table. When queried, it would look like:
Effect: Creates an index on the Department column, which improves query performance when searching or filtering by Department. This doesn't
change the visible structure of the table but affects how the database engine accesses the data.
ALTER
The ALTER command modifies existing database objects.
Adding a Column:
Before:
After:
Modifying a Column:
Effect: Changes the Department column's data type from VARCHAR(50) to VARCHAR(100), allowing longer department names.
Adding a Constraint:
Effect: Adds a check constraint to ensure that all salary values are at least 3000. This constraint is applied to both existing and future data.
DROP
The DROP command removes existing database objects.
Dropping a Table:
Effect: Completely removes the Employees table and all its data from the database.
Dropping a Database:
DROP DATABASE EmployeeManagement;
Effect: Completely removes the EmployeeManagement database and all its objects and data.
Dropping a View:
Effect: Removes the EmployeeSummary view from the database. This does not affect the underlying Employees table.
TRUNCATE
The TRUNCATE command quickly removes all records from a table without logging individual row deletions.
Effect:
Before:
After:
INSERT
The INSERT command adds new records to a table.
Effect:
Before:
After:
Effect:
Before:
After:
After:
Effect:
If we have:
PerformanceReviews table:
Effect:
Before:
After:
MERGE
The MERGE command (also called UPSERT in some systems) performs INSERT, UPDATE, and DELETE operations in a single statement based on a
condition.
Effect:
If we have:
NewEmployeesData table:
GRANT
The GRANT command gives specific privileges to users or roles.
Effect: Gives the HR_Staff role or user the ability to view (SELECT) and add (INSERT) records in the Employees table, but not UPDATE or DELETE
them.
Effect: Gives the Admin user full control over the CompanyDB database, including all objects within it.
REVOKE
The REVOKE command removes previously granted privileges from users or roles.
Effect: Removes the ability for HR_Staff to insert new records into the Employees table. They would still retain SELECT privileges if those were
previously granted.
Effect: Removes all privileges that the Analyst user had on the CompanyDB database.
DENY
The DENY command (available in some DBMS like SQL Server) explicitly prevents a user from having a specific privilege, even if granted through
group memberships.
Effect: Prevents HR_Staff from deleting records from the Employees table, even if they belong to another role that has DELETE privileges.
COMMIT
The COMMIT command permanently saves changes made during the current transaction.
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 1000 WHERE AccountID = 123;
UPDATE Accounts SET Balance = Balance + 1000 WHERE AccountID = 456;
COMMIT;
Effect:
Before:
ROLLBACK
The ROLLBACK command undoes changes made during the current transaction.
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 1000 WHERE AccountID = 123;
-- Oops, wrong account!
ROLLBACK;
Effect:
Before:
SAVEPOINT
The SAVEPOINT command creates points within a transaction to which you can later roll back.
BEGIN TRANSACTION;
INSERT INTO Orders (OrderID, CustomerID, Amount) VALUES (1001, 5, 150.00);
SAVEPOINT order_inserted;
INSERT INTO OrderItems (OrderID, ProductID, Quantity) VALUES (1001, 101, 2);
INSERT INTO OrderItems (OrderID, ProductID, Quantity) VALUES (1001, 102, 1);
-- Oops, wrong product!
ROLLBACK TO order_inserted;
INSERT INTO OrderItems (OrderID, ProductID, Quantity) VALUES (1001, 101, 2);
INSERT INTO OrderItems (OrderID, ProductID, Quantity) VALUES (1001, 103, 1);
COMMIT;
Effect:
Orders table:
OrderItems table:
Note that we rolled back to the savepoint, so the incorrect product entries were not saved.
SELECT
The SELECT statement is the most commonly used SQL command for retrieving data.
Effect: Retrieves all columns and all rows from the Employees table:
Basic Syntax
Column Aliases
Column aliases provide temporary names for columns in the result set, making the output more readable or meaningful.
SELECT
Name AS CustomerName,
Email AS ContactEmail,
Phone AS ContactPhone
FROM Customers;
Effect:
Calculated Columns
You can perform calculations in the SELECT clause to create derived columns.
SELECT
ProductName,
UnitPrice,
UnitsInStock,
UnitPrice * UnitsInStock AS InventoryValue
FROM Products;
Effect:
String Concatenation
You can combine text fields using string concatenation (the syntax varies by database system).
-- SQL Server / SQLite
SELECT
FirstName + ' ' + LastName AS FullName
FROM Employees;
-- MySQL / PostgreSQL
SELECT
CONCAT(FirstName, ' ', LastName) AS FullName
FROM Employees;
Effect:
FullName
John Doe
Jane Smith
Michael Johnson
Emily Williams
Basic Syntax
Comparison Operators
Effect:
ProductName UnitPrice
Product B 25.00
Equal to: =
Not equal to: <> or !=
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
String Comparisons
SELECT CustomerID, Name, City
FROM Customers
WHERE City = 'Chicago';
Effect:
Name Email
John Smith [email protected]
Jane Doe [email protected]
SELECT ProductName
FROM Products
WHERE ProductName LIKE '%A%';
ProductName
Product A
SELECT FirstName
FROM Employees
WHERE FirstName LIKE '_o%';
FirstName
John
Bob
Basic Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
ProductName UnitPrice
Product B 25.00
Product C 15.50
Product A 10.00
Product D 5.00
Effect: Sorts employees first by department (alphabetically) and then by salary (highest first) within each department:
Department
IT
HR
Finance
Marketing
Without DISTINCT, if there were multiple employees in each department, each department would appear multiple times in the result.
Multi-Column DISTINCT
City Country
New York USA
Chicago USA
London UK
Paris France
Effect:
NumberOfDepartments
4
Logical Operators
Logical operators allow you to combine multiple conditions in SQL queries, providing more complex filtering capabilities.
BETWEEN Operator
The BETWEEN operator selects values within a specified range (inclusive).
Syntax
ProductName UnitPrice
Product A 10.00
Product C 15.50
NOT BETWEEN
You can negate the BETWEEN operator:
Effect: Finds products with prices less than $10 or greater than $20:
ProductName UnitPrice
Product B 25.00
Product D 5.00
IN Operator
The IN operator allows you to specify multiple values in a WHERE clause, functioning as a shorthand for multiple OR conditions.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
Simple IN Example
IN with Subquery
The IN operator can also be used with a subquery:
CategoryID CategoryName
1 Beverages
2 Condiments
3 Confections
ProductName ProductID
Product A 101
Product B 102
Product D 104
NOT IN
You can negate the IN operator:
SELECT CustomerID, Name, City
FROM Customers
WHERE City NOT IN ('New York', 'Chicago');
AND Operator
The AND operator combines two or more conditions, and returns records that satisfy all conditions.
Syntax
Example
Effect: Finds products that cost more than $10 and have more than 20 units in stock:
Effect: Finds IT employees with salaries of at least $70,000 who were hired after January 1, 2020:
OR Operator
The OR operator combines two or more conditions, and returns records that satisfy at least one of the conditions.
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example
Effect: Finds products that either cost more than $20 or have more than 50 units in stock:
Effect: Finds employees in either IT or Finance departments with salaries over $75,000:
Without parentheses, the query might not produce the expected results due to operator precedence (AND typically has higher precedence than OR).
NOT Operator
The NOT operator negates a condition, returning records that do not match the condition.
Syntax
ProductName CategoryID
Product B 2
Product C 3
NOT with Other Operators
NOT can be combined with other operators:
Effect: Finds customers not from New York or Chicago, and whose names don't start with 'J':
NULL Values
NULL in SQL represents a missing or unknown value. Understanding how to work with NULL values is crucial for effective database querying and
management.
Effect: The Name and Email columns cannot contain NULL, but Phone and Address can.
Effect: Changes the Address column to disallow NULL values. This would fail if any existing rows contain NULL in the Address column.
Setting Default Values
You can provide default values for columns to avoid NULL values:
Effect: If OrderDate is not specified during insertion, it will get the current date instead of NULL.
Effect: Returns no rows, regardless of the data, because x = NULL evaluates to NULL (not TRUE) for any value of x.
COALESCE Function
The COALESCE function returns the first non-NULL value in a list:
SELECT
ProductName,
COALESCE(SupplierName, 'No Supplier') AS Supplier
FROM Products
LEFT JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID;
Effect: Shows supplier name or "No Supplier" for products without a supplier:
ProductName Supplier
Product A Supplier X
Product B No Supplier
Product C Supplier Y
Product D Supplier Z
NULLIF Function
The NULLIF function returns NULL if two expressions are equal, otherwise it returns the first expression:
SELECT
ProductName,
UnitsInStock,
ReorderLevel,
NULLIF(UnitsInStock, ReorderLevel) AS StockDifference
FROM Products;
Effect: Returns NULL when stock equals reorder level, otherwise returns stock level:
Effect:
If we have:
Employees table:
UPDATE
The UPDATE command modifies existing records in a table.
UPDATE Employees
SET Department = 'Information Technology'
WHERE Department = 'IT';
Effect:
Before:
After:
Effect:
Before:
After:
DELETE
The DELETE command removes records from a table.
Effect:
Before: