SQL Notes by Apna College
SQL Notes by Apna College
SQL In SQL, data types define the kind of data that can be stored in a column or variable.
What is DBMS?
DATATYPE DESCRIPTION USAGE
DBMS (Database Management System) is software used to create, manage, and organize
databases. CHAR string(0-255), can store characters of fixed length CHAR(50)
What is RDBMS? VARCHAR string(0-255), can store characters up to given length VARCHAR(50)
● RDBMS (Relational Database Management System) - is a DBMS based on the
BLOB string(0-65535), can store binary large object BLOB(1000)
concept of tables (also called relations).
● Data is organized into tables (also known as relations) with rows (records) and INT integer( -2,147,483,648 to 2,147,483,647 ) INT
columns (attributes).
● Eg - MySQL, PostgreSQL, Oracle etc. TINYINT integer(-128 to 127) TINYINT
*Note - SQL keywords are NOT case sensitive. Eg: select is the same as SELECT in SQL. TIME HH:MM:SS TIME
SQL v/s MySQL YEAR year in 4 digits format ranging from 1901 to 2155 YEAR
SQL is a language used to perform CRUD operations in Relational DB, while MySQL is a
RDBMS that uses SQL.
*Note - CHAR is for fixed length & VARCHAR is for variable length strings. Generally,
VARCHAR is better as it only occupies necessary memory & works more efficiently.
We can also use UNSIGNED with datatypes when we only have positive values to add.
Eg - UNSIGNED INT
DDL commands enable you to create, modify, and delete database objects like tables, ○ Used to remove an existing constraint from a table.
indexes, constraints, and more.
○ Example: ALTER TABLE orders DROP CONSTRAINT fk_customer;
Key DDL Commands are:
● TRUNCATE TABLE:
● CREATE TABLE:
○ Used to delete the data inside a table, but not the table itself.
○ Syntax – TRUNCATE TABLE table_name
○ Used to create a new table in the database.
○ Specifies the table name, column names, data types, constraints, and more.
○ Example:
CREATE TABLE employees (id INT PRIMARY KEY, name VARCHAR(50),
salary DECIMAL(10, 2));
2. DATA QUERY/RETRIEVAL LANGUAGE (DQL or DRL)
● ALTER TABLE:
DQL (Data Query Language) is a subset of SQL focused on retrieving data from databases.
○ Used to modify the structure of an existing table.
○ You can add, modify, or drop columns, constraints, and more. The SELECT statement is the foundation of DQL and allows us to extract specific columns
○ Example: ALTER TABLE employees ADD COLUMN email VARCHAR(100); from a table.
● DROP TABLE:
● SELECT:
○ Used to delete an existing table along with its data and structure.
○ Example: DROP TABLE employees; The SELECT statement is used to select data from a database.
SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND
Syntax: SELECT column1, column2, ... FROM table_name; condition3 ...;
Here, column1, column2, ... are the field names of the table. SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR
condition3 ...;
If you want to select all the fields available in the table, use the following syntax:
SELECT * FROM table_name; SELECT column1, column2, ... FROM table_name WHERE NOT condition;
Note: In some versions of SQL this operator may be written as != There are two wildcards often used in conjunction with the LIKE operator:
- The WHERE clause can be combined with AND, OR, and NOT operators. Example: SELECT * FROM employees WHERE first_name LIKE 'J%';
- The AND and OR operators are used to filter records based on more than one WHERE CustomerName LIKE 'a%'
condition: - Finds any values that start with "a"
- The AND operator displays a record if all the conditions separated by AND are WHERE CustomerName LIKE '%a'
TRUE. - Finds any values that end with "a"
- The OR operator displays a record if any of the conditions separated by OR is TRUE. WHERE CustomerName LIKE '%or%'
- Finds any values that have "or" in any position
- The NOT operator displays a record if the condition(s) is NOT TRUE.
WHERE CustomerName LIKE '_r%'
Syntax: - Finds any values that have "r" in the second position
WHERE CustomerName LIKE 'a_%' - Syntax: SELECT column1, column2 FROM table_name ORDER BY column1
- Finds any values that start with "a" and are at least 2 characters in length [ASC|DESC];
Checks for NULL values in the WHERE clause. - By default, NULL values are considered the smallest in ascending order and the
largest in descending order.
Example: SELECT * FROM customers WHERE email IS NULL; - You can control the sorting behaviour of NULL values using the NULLS FIRST or
NULLS LAST options.
- Example: SELECT column_name FROM table_name ORDER BY column_name
● AS: NULLS LAST;
Example: SELECT first_name AS "First Name", last_name AS "Last Name" FROM - Instead of specifying column names, you can sort by column positions in the ORDER
employees; BY clause.
- Example: SELECT product_name, price FROM products ORDER BY 2 DESC, 1
ASC;
● ORDER BY
The ORDER BY clause allows you to sort the result set of a query based on one or more ● GROUP BY
columns.
The GROUP BY clause in SQL is used to group rows from a table based on one or more
Basic Syntax: columns.
- The ORDER BY clause is used after the SELECT statement to sort query results. Syntax:
- The GROUP BY clause follows the SELECT statement and is used to group rows Computes the average of numeric values in a group or result set.
based on specified columns.
- MAX():
- Syntax: SELECT column1, aggregate_function(column2) FROM table_name Finds the maximum value in a group or result set.
GROUP BY column1;
- MIN():
- Aggregation Functions: Retrieves the minimum value in a group or result set.
○ Aggregation functions (e.g., COUNT, SUM, AVG, MAX, MIN) are often used
with GROUP BY to calculate values for each group.
○ Example: SELECT department, AVG(salary) FROM employees GROUP BY
department; 3. DATA MANIPULATION LANGUAGE
- Grouping by Multiple Columns:
○ You can group by multiple columns by listing them in the GROUP BY clause. Data Manipulation Language (DML) in SQL encompasses commands that manipulate data
○ This creates a hierarchical grouping based on the specified columns. within a database. DML allows you to insert, update, and delete records, ensuring the
○ Example: SELECT department, gender, AVG(salary) FROM employees accuracy and currency of your data.
GROUP BY department, gender;
● INSERT:
- HAVING Clause:
- The INSERT statement adds new records to a table.
○ The HAVING clause is used with GROUP BY to filter groups based on - Syntax: INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2,
aggregate function results. ...);
○ It's similar to the WHERE clause but operates on grouped data.
○ Example: SELECT department, AVG(salary) FROM employees GROUP BY - Example: INSERT INTO employees (first_name, last_name, salary) VALUES ('John',
department HAVING AVG(salary) > 50000; 'Doe', 50000);
These are used to perform calculations on groups of rows or entire result sets. They provide - The DELETE statement removes records from a table.
insights into data by summarising and processing information. - Syntax: DELETE FROM table_name WHERE condition;
- Example: DELETE FROM employees WHERE last_name = 'Doe';
Common Aggregate Functions:
- COUNT():
Counts the number of rows in a group or result set.
4. Data Control Language (DCL)
- SUM():
Calculates the sum of numeric values in a group or result set.
- AVG():
Data Control Language focuses on the management of access rights, permissions, and FROM user_or_role;
security-related aspects of a database system.
In this syntax:
DCL commands are used to control who can access the data, modify the data, or perform
administrative tasks within a database. ● privilege_type is the privilege or permission being revoked.
● object_name is the name of the database object from which the privilege is being
DCL is an important aspect of database security, ensuring that data remains protected and revoked.
only authorised users have the necessary privileges. ● user_or_role is the name of the user or role from which the privilege is being
revoked.
There are two main DCL commands in SQL: GRANT and REVOKE.
Example: Revoking the SELECT privilege on the "Employees" table from the "Analyst" user:
1. GRANT:
REVOKE SELECT ON Employees FROM Analyst;
The GRANT command is used to provide specific privileges or permissions to users or roles.
Privileges can include the ability to perform various actions on tables, views, procedures,
and other database objects. DCL and Database Security:
Syntax: DCL plays a crucial role in ensuring the security and integrity of a database system.
GRANT privilege_type By controlling access and permissions, DCL helps prevent unauthorised users from
ON object_name tampering with or accessing sensitive data. Proper use of GRANT and REVOKE commands
TO user_or_role; ensures that only users who require specific privileges can perform certain actions on
database objects.
In this syntax:
ROLLBACK TO before_withdrawal;
2. ROLLBACK:
-- The first update is still applied
The ROLLBACK command is used to undo changes made during a transaction.
It reverts all the changes applied to the database since the transaction began. COMMIT;
ROLLBACK is typically used when an error occurs during the execution of a transaction,
ensuring that the database remains in a consistent state. TCL and Transaction Management:
Example: Rolling back changes due to an error during a transaction: Transaction Control Language (TCL) commands are vital for managing the integrity and
consistency of a database's data.
BEGIN; They allow you to group related changes into transactions, and in the event of errors, either
commit those changes or roll them back to maintain data integrity.
UPDATE Inventory TCL commands are used in combination with Data Manipulation Language (DML) and other
SET Quantity = Quantity - 10 SQL commands to ensure that the database remains in a reliable state despite unforeseen
WHERE ProductID = 101; errors or issues.
ROLLBACK;
3. SAVEPOINT:
The SAVEPOINT command creates a named point within a transaction, allowing you to set a JOINS
point to which you can later ROLLBACK if needed.
SAVEPOINTs are useful when you want to undo part of a transaction while preserving other In a DBMS, a join is an operation that combines rows from two or more tables based on a
changes. related column between them.
Joins are used to retrieve data from multiple tables by linking them together using a common
Syntax: SAVEPOINT savepoint_name; key or column.
BEGIN;
1. Inner Join
2. Outer Join 101 1 Laptop
3. Cross Join
4. Self Join 102 3 Smartphone
An inner join combines data from two or more tables based on a specified condition, known Inner Join Query:
as the join condition.
The result of an inner join includes only the rows where the join condition is met in all SELECT Customers.CustomerName, Orders.Product
participating tables. FROM Customers
It essentially filters out non-matching rows and returns only the rows that have matching INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
values in both tables.
Result:
Syntax:
CustomerName Product
SELECT columns
FROM table1
Alice Laptop
INNER JOIN table2
ON table1.column = table2.column;
Bob Headphones
Here:
Carol Smartphone
● columns refers to the specific columns you want to retrieve from the tables.
● table1 and table2 are the names of the tables you are joining.
● column is the common column used to match rows between the tables.
● The ON clause specifies the join condition, where you define how the tables are 2) Outer Join
related.
Outer joins combine data from two or more tables based on a specified condition, just like
Example: Consider two tables: Customers and Orders. inner joins. However, unlike inner joins, outer joins also include rows that do not have
matching values in both tables.
Customers Table: Outer joins are particularly useful when you want to include data from one table even if there
is no corresponding match in the other table.
CustomerID CustomerName
Types:
1 Alice
There are three types of outer joins: left outer join, right outer join, and full outer join.
2 Bob
A left outer join returns all the rows from the left table and the matching rows from the right
Orders Table: table.
NULL Keyboard
CustomerName Product
Alice Laptop Here, the right outer join includes all rows from the Orders table. Since there is no matching
order for the customer with CustomerID 4, the result includes a row with NULL values in the
CustomerName column.
Bob Headphones
Carol Smartphone
3. Full Outer Join (Full Join):
NULL Monitor
A full outer join returns all rows from both the left and right tables, including matches and
non-matches.
In this example, the left outer join includes all rows from the Customers table.
If there's no match, NULL values appear in columns from the table where there's no
Since there is no matching customer for the order with OrderID 103 (Monitor), the result corresponding value.
includes a row with NULL values in the CustomerName column.
Example: Using the same Customers and Orders tables.
A right outer join is similar to a left outer join, but it returns all rows from the right table and Result:
the matching rows from the left table.
If there is no match in the left table, the result will still include the right table's row with NULL CustomerName Product
values in the left table's columns.
Alice Laptop
Example: Using the same Customers and Orders tables.
Result:
NULL Monitor 2 Bob
In this full outer join example, all rows from both tables are included in the result. Both CourseID CourseName
non-matching rows from the Customers and Orders tables are represented with NULL
values. 101 Maths
102 Science
3) Cross Join
Cross Join Query:
A cross join, also known as a Cartesian product, is a type of join operation in a Database SELECT Students.StudentName, Courses.CourseName
Management System (DBMS) that combines every row from one table with every row from FROM Students
another table. CROSS JOIN Courses;
Unlike other join types, a cross join does not require a specific condition to match rows Result:
between the tables. Instead, it generates a result set that contains all possible combinations
of rows from both tables.
StudentName CourseName
Cross joins can lead to a large result set, especially when the participating tables have many
rows. Alice Maths
Syntax:
Alice Science
SELECT columns
FROM table1 Bob Maths
CROSS JOIN table2;
Bob Science
In this syntax:
● columns refers to the specific columns you want to retrieve from the cross-joined In this example, the cross join between the Students and Courses tables generates all
tables. possible combinations of rows from both tables. As a result, each student is paired with each
● table1 and table2 are the names of the tables you want to combine using a cross course, leading to a total of four rows in the result set.
join.
1 Alice This technique is useful when a table contains hierarchical or related data and you need to
compare or analyse rows within the same table.
Self joins are commonly used to find relationships, hierarchies, or patterns within a single
table. Alice Carol
In a self join, you treat the table as if it were two separate tables, referring to them with Bob Carol
different aliases.
In this syntax:
● columns refers to the specific columns you want to retrieve from the self-joined table.
● table1 is the name of the table you're joining with itself.
● alias1 and alias2 are aliases you assign to the table instances for differentiation.
SET OPERATIONS
● column is the column you use as the join condition to link rows from the same table.
Set operations in SQL are used to combine or manipulate the result sets of multiple SELECT
Example: Consider an Employees table that contains information about employees and their
queries.
managers.
They allow you to perform operations similar to those in set theory, such as union,
intersection, and difference, on the data retrieved from different tables or queries.
Employees Table:
Set operations provide powerful tools for managing and manipulating data, enabling you to
EmployeeID EmployeeName ManagerID analyse and combine information in various ways.
2 Bob 3 ● UNION
● INTERSECT
3 Carol NULL ● EXCEPT (or MINUS)
● UNION ALL
4 David 1
SELECT e1.EmployeeName AS Employee, e2.EmployeeName AS Manager The UNION operator combines the result sets of two or more SELECT queries into a single
FROM Employees AS e1 result set.
JOIN Employees AS e2 ON e1.ManagerID = e2.EmployeeID; It removes duplicates by default, meaning that if there are identical rows in the result sets,
only one instance of each row will appear in the final result.
Result:
Example:
Employee Manager
Assume we have two tables: Customers and Suppliers.
Example: Using the same tables as before.
Customers Table:
SELECT CustomerName FROM Customers
CustomerID CustomerName INTERSECT
SELECT SupplierName FROM Suppliers;
1 Alice
Result:
2 Bob
CustomerName
Suppliers Table: In this example, there are no common names between customers and suppliers, so the
result is an empty set.
SupplierID SupplierName
The EXCEPT operator (also known as MINUS in some databases) returns the distinct rows
102 SupplierB
that are present in the result set of the first SELECT query but not in the result set of the
second SELECT query.
UNION Query:
Example: Using the same tables as before.
SELECT CustomerName FROM Customers
UNION SELECT CustomerName FROM Customers
SELECT SupplierName FROM Suppliers; EXCEPT
SELECT SupplierName FROM Suppliers;
Result:
CustomerName
Result:
Alice
CustomerName
Bob Alice
SupplierA Bob
SupplierB
In this example, the names "Alice" and "Bob" are customers but not suppliers, so they
appear in the result set.
2. INTERSECT:
4. UNION ALL:
The INTERSECT operator returns the common rows that exist in the result sets of two or
more SELECT queries. The UNION ALL operator performs the same function as the UNION operator but does not
remove duplicates from the result set. It simply concatenates all rows from the different
It only returns distinct rows that appear in all result sets. result sets.
Example: Using the same tables as before.
Result:
Bob
Usage Scenarios analysing related data from from different tables based on In this syntax:
different queries or tables. their relationships. ● columns refers to the specific columns you want to retrieve from the outer query.
● table is the name of the table you're querying.
● column is the column you're applying the operator to in the outer query.
● OPERATOR is a comparison operator such as =, >, <, IN, NOT IN, etc.
Result sets may have different Result sets can have different ● (SELECT column FROM table WHERE condition) is the subquery that provides the
Result Set input for the comparison.
column names, but data types and column names, data types, and
Structure Example: Consider two tables: Products and Orders.
counts must match. counts.
Products Table:
3 Headphones 50
Orders Table:
101 1 2
102 3 1
SUB QUERIES
For Example: Retrieve the product names and quantities for orders with a total cost greater
Subqueries, also known as nested queries or inner queries, allow you to use the result of than the average price of all products.
one query (the inner query) as the input for another query (the outer query).
SELECT ProductName, Quantity
Subqueries are often used to retrieve data that will be used for filtering, comparison, or FROM Products
calculation within the context of a larger query. WHERE Price * Quantity > (SELECT AVG(Price) FROM Products);
They are a way to break down complex tasks into smaller, manageable steps. Result:
Syntax:
ProductName Quantity
SELECT columns
Laptop 2
Not used for combining rows; Combines rows from different tables
Combining Rows
used to filter or evaluate data. based on specified join conditions.