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

Chapter 4- Structured Query Language (SQL)-PARTA notes

Uploaded by

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

Chapter 4- Structured Query Language (SQL)-PARTA notes

Uploaded by

sujeet.p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 117

Chapter-4

Structured Query Language


(SQL)
Dr. Jyoti Wadmare
SQL ( Structured Query Language)
History of SQL
SQL ( Structured Query Language)
SQL- What SQL can do
SQL- Sublanguages
Types of SQL Commands
Types of SQL Commands
Data Definition Language (DDL)
1. Create
II Create Table
SQL Data Types
SQL Constraints
SQL Constraints
2. Alter
1. Alter Table-Add column
2. Alter Table-drop column
3. Alter Table-modify column
3. Alter Table-Rename column
3. DROP
4. Truncate
5. Rename
DML Commands
1. SELECT
Where Clause
Basic SQL Structure
Optional Clauses in SELECT statement
SQL Functions
Aggregate functions
1. Count
2. SUM Function
3. AVG Function
4. MAX 5. MIN
Scalar Functions
1.UCASE
2. LCASE 3. MID
MID example
4. LENGTH
5. Round
6. NOW
7. FORMAT
Group By Clause
The GROUP BY and HAVING clauses are essential SQL concepts often used together to aggregate data

and filter groups based Purpose:

The GROUP BY clause is used to group rows that have

the same values in specified columns on certain conditions.

Usage: It is commonly used with aggregate functions such as COUNT(),

SUM(), AVG(), MAX(), and MIN()

Query:

SELECT department, COUNT(employee_id) as num_employees

FROM employees GROUP BY department;


Having Clause
HAVING Clause

● Purpose: The HAVING clause is used to filter groups after the GROUP BY clause has been applied.
● It is similar to the WHERE clause, but WHERE is used to filter rows before grouping, while HAVING is used to filter groups after
aggregation.
● Usage: HAVING is often used in conjunction with aggregate functions.

SELECT department, COUNT(employee_id) as num_employees

FROM employees GROUP BY department

HAVING COUNT(employee_id) > 2;


Example: Show class and subject whose avg score is>80
SELECT class, subject, AVG(score) as avg_score FROM students GROUP BY class, subject HAVING AVG(score) > 80;
Apply GROUP BY ,HAVING for finding total revenue of
category is greater than 5000
Query
SELECT category, SUM(quantity * price) as total_revenue

FROM sales

GROUP BY category

HAVING SUM(quantity * price) > 5000;


SQL Operators
SQL Arithmetic Operators
SQL Comparison Operators
SQL Logical Operators
SQL Special Operators
2. Insert
3. Update
Update Example
4. Delete
SQL AND OR NOT OPERATOR
AND Opertaor
Combining AND OR conditions
IN Operator

Subqueries:
Example
NOT IN Operator
Not BETWEEN Operator
Like Operator
SQL NULL Values
IS-NULL Operator
IS NOT NULL operator
INTEGRITY CONSTRAINTS
1. Domain Constraint
2. Entity integrity contraint
3. Referential integrity Constraint
Syntax Referential Integrity
CREATE TABLE Department ( -- Inserting into Department table
INSERT INTO Department (DeptID, DeptName)
DeptID INT PRIMARY KEY, VALUES (1, 'HR'), (2, 'IT');
DeptName VARCHAR(100)
-- Inserting into Employee table with valid DeptID
); INSERT INTO Employee (EmpID, EmpName, DeptID)
VALUES (101, 'John Doe', 1),
(102, 'Jane Smith', 2);
CREATE TABLE Employee (

EmpID INT PRIMARY KEY,


Example of a Query Violating the Integrity
EmpName VARCHAR(100),
Constraint:
-- This will fail because there is no DeptID =
DeptID INT,
3 in the Department table
FOREIGN KEY (DeptID) REFERENCES Department(DeptID) INSERT INTO Employee (EmpID, EmpName, DeptID)
);
VALUES (103, 'Alice Brown', 3);
4. Key Constraint
SET Operators
1. Union Operator
Example:
2. Intersection Operator
Example
3. SET DIFFERENCE OPERATOR
Example:
Data Control Language
Data Control Language
Data Control Language
Data Control Language
Data Control Language
Data Control Language
Data Control Language
View
Types of Views
Types of Views
1. Simple View
A simple view is created based on a single table and does not contain complex logic like joins, groupings, or subqueries.

Example:

Let's say we have a table employees with columns employee_id, name, department, and salary.

CREATE VIEW employee_view


AS
SELECT employee_id, name, department
FROM employees;
2. Complex View

A complex view is created from multiple tables or includes complex SQL operations such as JOINs, GROUP BY, HAVING, and
subqueries.

Example:

Let's consider two tables: employees and departments.

CREATE VIEW employee_department_view

AS

SELECT e.name, e.salary, d.department_name

FROM employees e

JOIN departments d ON e.department_id = d.department_id;


This complex view retrieves data by joining two tables (employees and departments).
A materialized view

A materialized view stores the result of a query physically on the disk. It is refreshed periodically and provides faster access to query
results compared to regular views.

Example:

CREATE MATERIALIZED VIEW department_avg_salary

AS

SELECT department, AVG(salary) AS avg_salary

FROM employees

GROUP BY department;

You might also like