0% found this document useful (0 votes)
5 views4 pages

SQL_Notes_and_Concepts

The document explains nested queries (subqueries) in SQL, detailing types such as single row, multiple row, multiple column, correlated subqueries, and their usage in various clauses. It also covers triggers, which are automatic instructions that execute on specific events, and the relational model with integrity constraints that ensure data accuracy and consistency. Additionally, it discusses the GROUP BY clause and the HAVING clause for filtering grouped results in SQL.

Uploaded by

keshav yadav
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)
5 views4 pages

SQL_Notes_and_Concepts

The document explains nested queries (subqueries) in SQL, detailing types such as single row, multiple row, multiple column, correlated subqueries, and their usage in various clauses. It also covers triggers, which are automatic instructions that execute on specific events, and the relational model with integrity constraints that ensure data accuracy and consistency. Additionally, it discusses the GROUP BY clause and the HAVING clause for filtering grouped results in SQL.

Uploaded by

keshav yadav
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/ 4

Nested Queries (Subqueries) in SQL

A nested query or subquery is a query within another query, enclosed in parentheses. Subqueries

help break down complex logic into simpler, modular parts.

1. Single Row Subquery:

Returns one row and one column.

Example:

SELECT Name FROM Students WHERE Marks = (SELECT MAX(Marks) FROM Students);

2. Multiple Row Subquery:

Returns multiple rows. Use with IN, ANY, or ALL.

Example:

SELECT Name FROM Students WHERE Course IN (SELECT Course FROM Courses WHERE

Faculty = 'Dr. Smith');

3. Multiple Column Subquery:

Returns multiple columns.

Example:

SELECT Name FROM Students WHERE (Age, Marks) = (SELECT MIN(Age), MAX(Marks) FROM

Students);

4. Correlated Subquery:

Depends on the outer query.

Example:

SELECT Name FROM Students S WHERE Marks > (SELECT AVG(Marks) FROM Students

WHERE Course = S.Course);


5. Subquery in FROM Clause:

SELECT Course, AVG_Marks FROM (SELECT Course, AVG(Marks) AS AVG_Marks FROM

Students GROUP BY Course) AS AvgTable WHERE AVG_Marks > 70;

6. Subquery in SELECT Clause:

SELECT Name, (SELECT Course FROM Courses WHERE Courses.Course_ID =

Students.Course_ID) AS CourseName FROM Students;

Triggers in SQL
A trigger is a set of instructions that automatically runs when a specific event happens in a table (like

INSERT, UPDATE, DELETE).

Example 1 - Log Insertions:

CREATE TRIGGER log_insert AFTER INSERT ON Students FOR EACH ROW BEGIN INSERT

INTO LogTable (Action, Time) VALUES ('New student added', NOW()); END;

Example 2 - Prevent Negative Balance:

CREATE TRIGGER check_balance BEFORE UPDATE ON Accounts FOR EACH ROW BEGIN IF

NEW.Balance < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Balance cannot be

negative'; END IF; END;

Relational Model & Integrity Constraints


Relational Model organizes data in tables called relations.

Key Terms:

- Relation: Table

- Tuple: Row
- Attribute: Column

- Domain: Valid data type or range

Integrity Constraints:

1. Domain Constraint - Ensures valid data types.

2. Entity Integrity - Primary key must be unique and not null.

3. Referential Integrity - Foreign key must match primary key in another table.

4. Key Constraint - Each table must have a unique key.

Example:

CREATE TABLE Students (StudentID INT PRIMARY KEY, Name VARCHAR(50), Age INT);

GROUP BY in SQL
GROUP BY groups rows with the same values in one or more columns. Often used with aggregate

functions.

Example:

SELECT Course, COUNT(*) AS Total_Students FROM Students GROUP BY Course;

HAVING Clause:

Used to filter grouped results.

Example:

SELECT Course, COUNT(*) FROM Students GROUP BY Course HAVING COUNT(*) > 5;

Enforcing Integrity Constraints in Relational Data


Integrity constraints ensure accuracy and consistency in relational databases.
1. Domain Integrity:

Example: CHECK (Age > 18)

2. Entity Integrity:

Example: PRIMARY KEY must be NOT NULL and UNIQUE

3. Referential Integrity:

Example: FOREIGN KEY (StudentID) REFERENCES Students(StudentID)

4. Key Integrity:

Example: Email VARCHAR(100) UNIQUE

5. User-Defined Integrity:

Example: Trigger to prevent negative balance

CREATE TRIGGER prevent_negative_balance BEFORE UPDATE ON Accounts FOR EACH ROW

BEGIN IF NEW.Balance < 0 THEN SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Balance

cannot be negative'; END IF; END;

You might also like