0% found this document useful (0 votes)
3 views26 pages

DBMS 2

The document provides an overview of advanced SQL concepts and database design, covering SQL basics, joins, aggregation functions, subqueries, views, indexes, set operators, date/time and string functions, embedded SQL, assertions, and triggers. It includes syntax examples and explanations for each topic. The content is structured to facilitate understanding of how to manage and manipulate databases effectively.

Uploaded by

harkesh.181312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views26 pages

DBMS 2

The document provides an overview of advanced SQL concepts and database design, covering SQL basics, joins, aggregation functions, subqueries, views, indexes, set operators, date/time and string functions, embedded SQL, assertions, and triggers. It includes syntax examples and explanations for each topic. The content is structured to facilitate understanding of how to manage and manipulate databases effectively.

Uploaded by

harkesh.181312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

DATABASE MANAGEMENT SYSTEM MODULE

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.

SELECT column1, column2, ... SELECT column1, column2


FROM table_name
FROM table_name; WHERE condition;
DATABASE MANAGEMENT SYSTEM MODULE

SQL BASICS
3. ORDER BY Clause

Used to sort the result-set by one or more columns.

Syntax:

SELECT column1, column2


FROM table_name
ORDER BY column1 ASC|DESC;
DATABASE MANAGEMENT SYSTEM MODULE

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

5. LEFT (OUTER) JOIN


Returns all records from the left table, and the matched records from the
right table.
SELECT employees.name, departments.dept_name
FROM employees
LEFT JOIN departments ON employees.dept_id = departments.dept_id;
DATABASE MANAGEMENT SYSTEM MODULE

SQL BASICS
JOINS

RIGHT (OUTER) JOIN

Returns all records from the right table, and the matched records from the
left table.

SELECT employees.name, departments.dept_name


FROM employees
RIGHT JOIN departments ON employees.dept_id = departments.dept_id;
DATABASE MANAGEMENT SYSTEM MODULE

SQL BASICS
JOINS
SELF JOIN

A table is joined with itself.

SELECT A.name AS Employee, B.name AS Manager


FROM employees A, employees B
WHERE A.manager_id = B.emp_id;
DATABASE MANAGEMENT SYSTEM MODULE

Aggregation Function
Used to perform calculations on multiple rows of a table's column.

COUNT(): Number of rows

SUM(): Total sum

AVG(): Average value

MAX(): Highest value

MIN(): Lowest value


DATABASE MANAGEMENT SYSTEM MODULE

Aggregation Function
GROUP BY Clause

Groups rows that have the same values into summary rows.

SELECT column1, COUNT(*)


FROM table_name
GROUP BY column1;
DATABASE MANAGEMENT SYSTEM MODULE

Aggregation Function
HAVING Clause

Used to filter groups after aggregation (like WHERE for GROUP BY)

SELECT column, COUNT(*)


FROM table
GROUP BY column
HAVING COUNT(*) > 5;
DATABASE MANAGEMENT SYSTEM MODULE

Subqueries
A query inside another query.

Types:

Scalar Subquery (returns a single value)

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

Views, Indexes, Set Operators


A View in SQL is essentially a virtual table that is created by writing a SQL query. It
doesn’t store the data physically like a regular table, instead, it dynamically shows
data based on the underlying query whenever you access it.

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

Views, Indexes, Set Operators


CREATE VIEW CustomerOrdersView AS
SELECT
C.customer_id,
C.first_name,
| C.last_name,
C.country,
O.order_id,
O.item,
O.amount
FROM Customers c
INNER JOIN Orders o ON c.customer_id = o.customer_id;
DATABASE MANAGEMENT SYSTEM MODULE

Views, Indexes, Set Operators


An Index is a data structure in a database
that speeds up data retrieval (i.e., SELECT CREATE TABLE Students (
queries) from large tables, similar to the StudentID INT PRIMARY KEY,
Name VARCHAR(100),
index of a book, which helps you quickly find
Grade INT
topics instead of reading every page. );
When a table has a lot of rows, searching
CREATE INDEX idx_name ON
without an index means the database
Students(Name);
checks each row one by one (full table
scan), which is slow. An index helps it jump
directly to the matching data.
DATABASE MANAGEMENT SYSTEM MODULE

Views, Indexes, Set Operators


Set Operators are used to combine the results of two or more SELECT
queries into a single result. They work like math sets, combining rows from
multiple queries.
DATABASE MANAGEMENT SYSTEM MODULE

Views, Indexes, Set Operators

Operator Description

UNION Combines results from both queries, removes


duplicates.

UNION ALL Combines results from both queries, includes


duplicates.

INTERSECT Returns only common rows from both queries.

EXCEPT (or MINUS in some DBMS) Returns rows from the first query that are not in the
second.
DATABASE MANAGEMENT SYSTEM MODULE

Views, Indexes, Set Operators


1. UNION : Combines column
SELECT column FROM table_1
names from both countries,
removes duplicates. UNION / UNION ALL / INTERSECT /
2. UNION ALL: Same as above, but EXCEPT
keeps all duplicates.
SELECT column FROM table_2;
3. INTERSECT: Returns only names
that exist in both tables.
4. EXCEPT : Returns names that
are in 1st table but not in 2nd
table.
DATABASE MANAGEMENT SYSTEM MODULE

Date/Time Functions
Function Description

NOW() Current date and time

CURDATE() Current date

CURTIME() Current time

DATE_ADD() Add time to date

DATEDIFF() Difference between two dates

YEAR(), MONTH() Extract part of date

SELECT DATEDIFF('2025-06-01', '2025-05-26');


DATABASE MANAGEMENT SYSTEM MODULE

String Functions
Function Description

CONCAT() Concatenate strings

LENGTH() Length of string

LOWER(), UPPER() Convert case

SUBSTRING() Extract substring

REPLACE() Replace part of string

TRIM() Remove whitespace


DATABASE MANAGEMENT SYSTEM MODULE

String Functions

SELECT CONCAT(first_name, ' ', last_name) AS full_name

FROM employees;
DATABASE MANAGEMENT SYSTEM MODULE

Embedded SQL
Embedded SQL means writing SQL statements inside another programming
language like C, Java, or Python.

It allows your program to interact with a database, such as retrieving, inserting,


updating, or deleting data , directly from the code.

Languages like C or Java don’t understand SQL by themselves. So, embedded


SQL bridges the gap between your programming logic and the database.
DATABASE MANAGEMENT SYSTEM MODULE

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

AFTER UPDATE ON employees

FOR EACH ROW

BEGIN

IF OLD.salary != NEW.salary THEN

INSERT INTO salary_log (emp_id, old_salary, new_salary)

VALUES (OLD.emp_id, OLD.salary, NEW.salary);

END IF;

END;
DATABASE MANAGEMENT SYSTEM MODULE

THANK YOU !!!

You might also like