Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

SQL Notes (2)

This document provides an overview of SQL, detailing its three major sub-languages: DDL, DML, and DCL, along with core operations like CRUD. It covers identifiers, data types, NULL values, basic SQL operations, and various clauses such as WHERE, ORDER BY, and GROUP BY, including examples for clarity. Additionally, it discusses updating and deleting data, as well as constraints like PRIMARY KEY and FOREIGN KEY to ensure data integrity.

Uploaded by

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

SQL Notes (2)

This document provides an overview of SQL, detailing its three major sub-languages: DDL, DML, and DCL, along with core operations like CRUD. It covers identifiers, data types, NULL values, basic SQL operations, and various clauses such as WHERE, ORDER BY, and GROUP BY, including examples for clarity. Additionally, it discusses updating and deleting data, as well as constraints like PRIMARY KEY and FOREIGN KEY to ensure data integrity.

Uploaded by

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

SQL Notes – Microsoft FRYSP

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.

SQL comprises of 3 major sub-languages:


1. Data Definition Language (DDL): to create and modify the structure of the database.
2. Data Manipulation Language (DML): to perform Read, Insert, Update and Delete operations on the data of the
database.
3. Data Control Language (DCL): to control the access of the data stored in the database.

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.

 MS SQL: @, $, #, and other Unicode letters


 MySQL: $
 Oracle: $, #, and other letters from database character set
 PostgreSQL: $, and other Unicode letters

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.

Kedar Joshi, NIrmaan Org. Page 1


SQL Notes – Microsoft FRYSP

Data Types
• INT: Integer numbers.

• FLOAT: Floating point numbers.

• DECIMAL: Fixed point numbers.

• CHAR(size): Fixed length string.

• VARCHAR(size): Variable length string

• DATE: Date values.

• TIME: Time values.

• DATETIME: Date and time values.

• BOOLEAN: True/false values.

• BLOB: Binary large objects.

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.

SELECT * FROM Employees WHERE ManagerId IS NULL ;


SELECT * FROM Employees WHERE ManagerId IS NOT NULL ;

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.

Basic SQL operations

CRUD (Create, Read, Update, Delete).


Common SQL statements: SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, DROP.

SELECT STATEMENT:

Kedar Joshi, NIrmaan Org. Page 2


SQL Notes – Microsoft FRYSP

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.

SELECT column1, column2


FROM table_name;

SELECT column1, column2, ...


FROM table_name;

SELECT name, age, major


FROM students;

SELECT * FROM table_name;


SELECT * FROM students;

Filtering Data with WHERE Clause


The WHERE clause in SQL is used to filter records that fulfill a specified condition. This clause is used to extract
only those records that meet certain criteria.

SELECT column1, column2, ...


FROM table_name
WHERE condition;

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

1. Basic WHERE Clause:

SELECT * FROM employees


WHERE age > 30;

This query selects all columns from the employees table where the age of the employee is greater than
30.

Kedar Joshi, NIrmaan Org. Page 3


SQL Notes – Microsoft FRYSP

2. Using AND and OR Operators:

SELECT first_name, last_name FROM employees


WHERE age > 30 AND department = 'HR';

This query selects the first and last names of employees who are older than 30 and work in the HR
department.

3. Using BETWEEN Operator:

SELECT first_name, last_name, salary FROM employees


WHERE salary BETWEEN 50000 AND 100000;

This query selects the first name, last name, and salary of employees whose salary is between 50,000
and 100,000.

4. Using LIKE Operator:

SELECT first_name, last_name FROM employees


WHERE last_name LIKE 'Sm%';

This query selects the first and last names of employees whose last names start with 'Sm'.

5. Using IN Operator:

SELECT first_name, last_name FROM employees


WHERE department IN ('HR', 'Engineering', 'Marketing');

This query selects the first and last names of employees who work in the HR, Engineering, or Marketing
departments.

Using ORDER BY to Sort Results


The ORDER BY clause is used to sort the result set of a query by one or more columns. The default order is
ascending (ASC), but it can also be specified as descending (DESC).

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

Kedar Joshi, NIrmaan Org. Page 4


SQL Notes – Microsoft FRYSP

Example Code
1. Basic ORDER BY:

SELECT first_name, last_name, age FROM employees


ORDER BY last_name;

This query selects the first name, last name, and age of employees, and sorts the results by last
name in ascending order.

2. ORDER BY Multiple Columns:

SELECT first_name, last_name, department FROM employees


ORDER BY department ASC, last_name DESC;

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.

3. ORDER BY with WHERE Clause:

SELECT first_name, last_name, age FROM employees


WHERE age > 30
ORDER BY age DESC;

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.

4. ORDER BY with LIMIT Clause:

SELECT first_name, last_name, salary FROM employees


ORDER BY salary DESC
LIMIT 5;

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.

Combining WHERE and ORDER BY


The WHERE clause can be used in conjunction with the ORDER BY clause to filter and then sort the result set.

Example Code
1. Filter and Sort:

SELECT first_name, last_name, department FROM employees

Kedar Joshi, NIrmaan Org. Page 5


SQL Notes – Microsoft FRYSP

WHERE department = 'Engineering'


ORDER BY last_name;

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.

2. Filter, Sort, and Limit:

SELECT first_name, last_name, salary FROM employees


WHERE department = 'HR' AND salary > 50000
ORDER BY salary DESC
LIMIT 3;

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.

Using Aggregate Functions (COUNT, SUM, AVG, MAX, MIN)


Aggregate functions in SQL perform calculations on a set of values and return a single value. They are
commonly used in conjunction with the GROUP BY clause to perform calculations on each group of values.

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';

This query counts the number of employees in the Engineering department.

SUM()
The SUM() function returns the total sum of a numeric column.

Syntax
SELECT SUM(column_name)
FROM table_name

Kedar Joshi, NIrmaan Org. Page 6


SQL Notes – Microsoft FRYSP

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.

Kedar Joshi, NIrmaan Org. Page 7


SQL Notes – Microsoft FRYSP

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.

Grouping Data with GROUP BY


The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of
customers in each country." The GROUP BY statement is often used with aggregate functions to perform
calculations on each group of values.

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

Example Code

1. Basic GROUP BY:

SELECT department, COUNT(employee_id) AS total_employees


FROM employees
GROUP BY department;

This query counts the number of employees in each department.

2. GROUP BY with Multiple Columns:

SELECT department, job_title, AVG(salary) AS average_salary


FROM employees
GROUP BY department, job_title;

This query calculates the average salary for each job title within each department.

3. GROUP BY with HAVING Clause:

Kedar Joshi, NIrmaan Org. Page 8


SQL Notes – Microsoft FRYSP

The HAVING clause is used to filter groups based on conditions. It's similar to the WHERE clause but is
used for groups.

SELECT department, SUM(salary) AS total_salary


FROM employees
GROUP BY department
HAVING SUM(salary) > 500000;

This query calculates the total salary for each department and only includes departments where the
total salary is greater than 500,000.

4. GROUP BY with JOIN:

SELECT d.department_name, COUNT(e.employee_id) AS total_employees


FROM employees e
JOIN departments d ON e.department_id = d.department_id
GROUP BY d.department_name;

This query joins the employees and departments tables, then counts the number of employees in each
department.

Combining Aggregate Functions with GROUP BY


Aggregate functions are often used in conjunction with the GROUP BY clause to provide meaningful summaries
of data.

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.

Updating Existing Data (UPDATE)


The UPDATE statement in SQL is used to modify the existing records in a table. It can update one or more
columns in one or more rows at once.

Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Kedar Joshi, NIrmaan Org. Page 9


SQL Notes – Microsoft FRYSP

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.

2. Updating Multiple Columns:

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.

3. Updating Multiple Rows:

UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';

This query increases the salary by 10% for all employees in the Engineering department.

4. Using Subquery in UPDATE:

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.

Deleting Data (DELETE)


The DELETE statement in SQL is used to remove existing records from a table.

Kedar Joshi, NIrmaan Org. Page 10


SQL Notes – Microsoft FRYSP

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:

DELETE FROM employees


WHERE employee_id = 103;

This query deletes the record of the employee with employee_id 103.

2. Deleting Multiple Rows:

DELETE FROM employees


WHERE department = 'HR';

This query deletes all employees who work in the HR department.

3. Using Subquery in DELETE:

DELETE FROM employees


WHERE salary < (SELECT AVG(salary) FROM employees);

This query deletes all employees whose salary is below the average salary of all employees.

Understanding and Using Constraints


Constraints in SQL are rules enforced on data columns on a table. They ensure the accuracy and reliability of
the data within the database. Constraints can be column-level or table-level, applied to a single column or
across multiple columns, respectively.

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).

Kedar Joshi, NIrmaan Org. Page 11


SQL Notes – Microsoft FRYSP

Syntax
CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
column2 datatype
);

-- Composite Primary Key


CREATE TABLE table_name (
column1 datatype,
column2 datatype,
PRIMARY KEY (column1, column2)
);

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)
);

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50),

Kedar Joshi, NIrmaan Org. Page 12


SQL Notes – Microsoft FRYSP

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
);

-- Table-Level Unique Constraint


CREATE TABLE table_name (
column1 datatype,
column2 datatype,
UNIQUE (column1, column2)
);

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,

Kedar Joshi, NIrmaan Org. Page 13


SQL Notes – Microsoft FRYSP

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)
);

-- Column-Level Check Constraint


CREATE TABLE table_name (
column1 datatype CHECK (condition),
column2 datatype
);

Example Code

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
age INT,
CHECK (age >= 18)
);

This example ensures that the age column in the employees table has values greater than or equal to 18.

Adding Constraints to Existing Tables

Kedar Joshi, NIrmaan Org. Page 14


SQL Notes – Microsoft FRYSP

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

1. Adding a UNIQUE Constraint:

ALTER TABLE employees


ADD CONSTRAINT unique_email UNIQUE (email);

2. Adding a FOREIGN KEY Constraint:

ALTER TABLE employees


ADD CONSTRAINT fk_department
FOREIGN KEY (department_id) REFERENCES departments(department_id);

3. Adding a CHECK Constraint:

ALTER TABLE employees


ADD CONSTRAINT check_age CHECK (age >= 18);

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:

ALTER TABLE employees


DROP CONSTRAINT unique_email;

2. Dropping a FOREIGN KEY Constraint:


ALTER TABLE employees
DROP CONSTRAINT fk_department;

3. Dropping a CHECK Constraint:

Kedar Joshi, NIrmaan Org. Page 15


SQL Notes – Microsoft FRYSP

ALTER TABLE employees


DROP CONSTRAINT check_age;

Differences Between Primary Key and Unique Key


Both primary keys and unique keys are used to ensure the uniqueness of the values in one or more columns of
a table. However, there are key differences between them:

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.

5. Number of Keys Allowed:


o Primary Key: Only one primary key is allowed per table.
o Unique Key: Multiple unique keys are allowed per table.

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.

Kedar Joshi, NIrmaan Org. Page 16


SQL Notes – Microsoft FRYSP

Ensuring Data Integrity


When updating or deleting data, it's crucial to ensure that the integrity of the data is maintained. This involves
understanding relationships and dependencies between tables.

Transactions and Rollback

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

Transactions have four key properties, often abbreviated as ACID:

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;

Rolling Back a Transaction


If an error occurs or if the operations need to be undone, use the ROLLBACK statement to revert the database to
its previous state.

Syntax

Kedar Joshi, NIrmaan Org. Page 17


SQL Notes – Microsoft FRYSP

ROLLBACK;

Example Code

Basic Transaction:

BEGIN TRANSACTION;

UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';

DELETE FROM employees


WHERE employee_id = 104;

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.

Transaction with Rollback:

BEGIN TRANSACTION;

UPDATE employees
SET salary = salary * 1.1
WHERE department = 'Engineering';

DELETE FROM employees


WHERE employee_id = 104;

-- Simulating an error
IF (SELECT COUNT(*) FROM employees WHERE employee_id = 104) > 0
BEGIN
ROLLBACK;
END
ELSE
BEGIN
COMMIT;
END;

Kedar Joshi, NIrmaan Org. Page 18


SQL Notes – Microsoft FRYSP

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;

DELETE FROM employees


WHERE employee_id = 104;

-- 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.

Transaction Isolation Levels

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

SET TRANSACTION ISOLATION LEVEL isolation_level;

Understanding Relationships Between Tables

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.

o Example: A person and their unique passport.

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.

o Example: A customer and their orders.

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

 Primary Key: Uniquely identifies each record within a table.

 Foreign Key: A column or set of columns in one table that refers to the primary key in another table.

Introduction to INNER JOIN

Kedar Joshi, NIrmaan Org. Page 20


SQL Notes – Microsoft FRYSP

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;

Basic INNER JOIN Queries

Example Tables
Let's consider two tables: employees and departments.

CREATE TABLE departments (


department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);

CREATE TABLE employees (


employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);

LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN

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

Kedar Joshi, NIrmaan Org. Page 21


SQL Notes – Microsoft FRYSP

SELECT employees.first_name, employees.last_name, departments.department_name


FROM employees
LEFT JOIN departments
ON employees.department_id = departments.department_id;

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

SELECT employees.first_name, employees.last_name, departments.department_name


FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.department_id;

This query retrieves all departments and their employees. If a department does not have any employees, the
employee details will be NULL.

FULL OUTER JOIN


A FULL OUTER JOIN returns all rows when there is a match in either left or right table. If there is no match, the
result is NULL from the side where there is no match.

Syntax
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;

SELECT columns
FROM table1
FULL OUTER JOIN table2

Kedar Joshi, NIrmaan Org. Page 22


SQL Notes – Microsoft FRYSP

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:

SELECT first_name, last_name


FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location = 'New York');

This query retrieves the names of employees who work in departments located in New York.

Kedar Joshi, NIrmaan Org. Page 23


SQL Notes – Microsoft FRYSP

3. Scalar Subquery:

SELECT first_name, last_name, (SELECT department_name FROM departments WHERE


departments.department_id = employees.department_id) AS department_name
FROM employees;

This query retrieves the names of employees along with their department names.

4. Correlated Subquery:

SELECT e1.first_name, e1.last_name, e1.salary


FROM employees e1
WHERE e1.salary > (SELECT AVG(e2.salary) FROM employees e2 WHERE e2.department_id =
e1.department_id);

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

CREATE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;

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

Kedar Joshi, NIrmaan Org. Page 24


SQL Notes – Microsoft FRYSP

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

SELECT * FROM view_name;

Example

SELECT * FROM employee_details;

This query retrieves all data from the employee_details view.

Syntax

CREATE OR REPLACE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;

Example

CREATE OR REPLACE VIEW employee_details AS


SELECT e.employee_id, e.first_name, e.last_name, d.department_name, d.location
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;

This updates the employee_details view to include the location column from the departments table.

Dropping a View

DROP VIEW view_name;

Example

DROP VIEW employee_details;

This removes the employee_details view from the database.

Kedar Joshi, NIrmaan Org. Page 25

You might also like