DBMS 2
DBMS 2
Advanced SQL
&
Database Design
DATABASE MANAGEMENT SYSTEM MODULE
SQL BASICS
1. SELECT Statement 2. WHERE Clause
Used to retrieve data from one Used to filter records that meet
or more tables. a specified condition.
SQL BASICS
3. ORDER BY Clause
Syntax:
SQL BASICS
JOINS
4. INNER JOIN
Returns records that have matching values in both tables.
SELECT a.col1, b.col2
FROM tableA a
INNER JOIN tableB b ON a.common_column = b.common_column;
DATABASE MANAGEMENT SYSTEM MODULE
SQL BASICS
JOINS
SQL BASICS
JOINS
Returns all records from the right table, and the matched records from the
left table.
SQL BASICS
JOINS
SELF JOIN
Aggregation Function
Used to perform calculations on multiple rows of a table's column.
Aggregation Function
GROUP BY Clause
Groups rows that have the same values into summary rows.
Aggregation Function
HAVING Clause
Used to filter groups after aggregation (like WHERE for GROUP BY)
Subqueries
A query inside another query.
Types:
Row Subquery
Table Subquery
Correlated Subquery
DATABASE MANAGEMENT SYSTEM MODULE
Subqueries
Example 1: Subquery in WHERE Example 1: Subquery in FROM
SELECT name
FROM employees SELECT AVG(salary)
WHERE dept_id = (SELECT FROM (SELECT salary FROM
dept_id FROM departments employees WHERE department =
WHERE dept_name = 'HR'); 'IT') AS it_salaries;
DATABASE MANAGEMENT SYSTEM MODULE
A view is a named SQL query stored in the database that acts like a table. When
you query a view, the database executes the underlying query and returns the
result.
DATABASE MANAGEMENT SYSTEM MODULE
Operator Description
EXCEPT (or MINUS in some DBMS) Returns rows from the first query that are not in the
second.
DATABASE MANAGEMENT SYSTEM MODULE
Date/Time Functions
Function Description
String Functions
Function Description
String Functions
FROM employees;
DATABASE MANAGEMENT SYSTEM MODULE
Embedded SQL
Embedded SQL means writing SQL statements inside another programming
language like C, Java, or Python.
Assertions
An Assertion is a constraint that you define at the database level to ensure that
certain conditions always hold true across one or more tables.
It enforces business rules or integrity rules that cannot be easily reinforced using
basic constraints like CHECK, NOT NULL, or FOREIGN KEY.
DATABASE MANAGEMENT SYSTEM MODULE
Triggers
A Trigger is a stored procedure that is automatically executed (or “fired”) by the
database when a specific event occurs on a table, such as:
● INSERT
● UPDATE
● DELETE
DATABASE MANAGEMENT SYSTEM MODULE
Triggers
CREATE TRIGGER log_salary_change
BEGIN
END IF;
END;
DATABASE MANAGEMENT SYSTEM MODULE