0% found this document useful (0 votes)
13 views

DBMS - Module - 3 Part 2

Uploaded by

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

DBMS - Module - 3 Part 2

Uploaded by

ythombare2000
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 96

Structured Query

Language (SQL)
& Indexing
-Unit 4
Overview of SQL
• Structured Query Language or SQL is a standard Database language
which is used to create, maintain and retrieve the data from
relational databases.
• Examples of databases are MySQL, Oracle, SQL Server, PostGre, etc.
The recent ISO standard version of SQL is SQL:2019.
• As the name suggests, it is used when we have structured data (in the
form of tables).
• All databases that are not relational (or do not use fixed structure
tables to store data) and therefore do not use SQL, are called NoSQL
databases. Examples of NoSQL are MongoDB, DynamoDB, Cassandra,
etc.
• SQL is used to perform operations on the records stored in the database,
such as
• updating records,
• inserting records,
• deleting records,
• creating and
• modifying database tables,
• views, etc.
• SQL is not case sensitive. But it is a recommended practice to use keywords
(like SELECT, UPDATE, CREATE, etc) in capital letters and use user defined
things (liked table name, column name, etc) in small letters.
Advantages of SQL

• Faster Query Processing (High speed)–


• Large amount of data is retrieved quickly and efficiently.
• Operations like Insertion, deletion, manipulation of data is also done in
almost no time.
• No Coding Skills –
• For data retrieval, large number of lines of code is not required.
• All basic keywords such as SELECT, INSERT INTO, UPDATE, etc are used
• Standardized Language –
• Due to documentation and long establishment over years, it provides a
uniform platform worldwide to all its users.
• Portable –
• It can be used in programs in PCs, server, laptops independent of any platform
(Operating System, etc).
• Also, it can be embedded with other applications as per need/requirement/use.

• Interactive Language –
• Easy to learn and understand, answers to complex queries can be received in
seconds.

• Multiple data view


• Using the SQL language, the users can make different views of the database
structure.
SQL Data types
SQL
Operators
• Arithmetic Operators • Comparison Operator
• Compound Operators • Bitwise Operator
Logical Operators
What is Relational Database?
• Relational database means the data is stored as well as retrieved in
the form of relations (tables).
• Table 1 shows the relational database with only one relation
called STUDENT,
• which stores ROLL_NO, NAME, ADDRESS, PHONE and AGE of
students.
• STUDENT
Important terminologies that are used in
terms of relation.
• Attribute (field): Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME etc.

• Tuple: Each row in the relation is known as tuple.

• Degree: The number of attributes in the relation is known as degree of the relation.
Ex.The STUDENT relation defined above has degree 5.

• Cardinality: The number of tuples in a relation is known as cardinality. Ex.The STUDENT relation
defined above has cardinality 3.

• Column: Column represents the set of values for a particular attribute.

• A query: It is an inquiry to the database for information.


Classification of
SQL commands
• DDL (Data Definition Language)

• DML (Data Manipulation Language)

• DCL (Data Control Language)

• DQL (Data Query Language)

• Transactional control commands


DQL (Data Query Language):

• It allows getting data from the database and imposing order upon it.
• It includes the SELECT statement.
• This command allows getting the data out of the database to perform
operations with it.

List of DQL:
• SELECT: It is used to retrieve data from the database.
• It is also can be used with where clause to retrieve specific record.
• Syntax: Select column from <table_name> where <condition>;
• Ex. Select * from students where rno=5;
SELECT Command
• The SELECT command shows the records of the specified table.
• It also shows the particular record of a particular column by using the WHERE clause.
• Select statement retrieves the data from database according to the constraints specifies
alongside.
• Syntax:
• SELECT <Col1 , col2 , col3 … , col N> FROM <TABLE NAME>
• Here, column_Name_1, column_Name_2, ….., column_Name_N are the names of those
columns whose data we want to retrieve from the table.
• If we want to retrieve the data from all the columns of the table, we have to use the
following SELECT command:
• SELECT * FROM table_name;
• Example:
• Select * from students;
USE OF DISTINCT
■ SQL does not treat a relation as a set; duplicate tuples can appear
■ To eliminate duplicate tuples in a query result, the keyword DISTINCT is used
■ For example, the result of Q11 may have duplicate SALARY values whereas
Q11A does not have any duplicate values

Q11:SELECT SALARY
FROM EMPLOYEE
Q11A: SELECT DISTINCT SALARY
FROM EMPLOYEE

Slide 8- 16
USE OF DISTINCT
■ SQL does not treat a relation as a set; duplicate tuples can appear
■ To eliminate duplicate tuples in a query result, the keyword DISTINCT is used
■ For example, the result of Q11 may have duplicate SALARY values whereas
Q11A does not have any duplicate values

Q11:SELECT SALARY
FROM EMPLOYEE
Q11A: SELECT DISTINCT SALARY
FROM EMPLOYEE

Slide 8- 17
Classification of constraints
SQL constraints
• Integrity constraints are a set of rules. It is used to maintain the quality of
information.
• Integrity constraints ensure that changes made to the database by authorized
users do not result in a loss of data consistency.
• Thus, integrity constraints guard against accidental damage to the database.
• Constraints can be specified when a table is created with the CREATE TABLE
statement.
• you can use the ALTER TABLE statement to create constraints even after the table
is created.
• Constraints can be defined in two ways
• column-level definition The constraints can be specified immediately after the column
definition.
• table-level definition The constraints can be specified after all the columns are defined.
NOT NULL Constraints
• The NOT NULL constraint in a column means that the column cannot
store NULL values.
• That is, you will be not allowed to insert a new row in the table
without specifying any value to this field.
Primary key constraints

• This constraint defines a column or combination of


columns which uniquely identifies each row in the table.

• Primary keys must contain UNIQUE values, and cannot


contain NULL values. Column Level
Column
• A table can have only ONE primary key; and in the table,
this primary key can consist of single or multiple columns
(fields).

• Syntax:
• It creates a PRIMARY KEY on the "ID" column when the
"Persons" table is created:
Table level
Foreign Key constraints
• The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
• A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in
another table.
• The table with the foreign key is called the child table, and the table with the primary key is called
the referenced or parent table.
Syntax:
• SQL FOREIGN KEY on CREATE TABLE
CREATE TABLE customer (
customerID int NOT NULL,
FirstName varchar(20),
LastName varchar(20)
City int,
PRIMARY KEY (customerID),
FOREIGN KEY (city) REFERENCES City(CityID)
);
Unique constraints
• The UNIQUE constraint ensures that all values in a column are
different.
• Both the UNIQUE and PRIMARY KEY constraints provide a guarantee
for uniqueness for a column or set of columns.
• A PRIMARY KEY constraint automatically has a UNIQUE constraint.
• However, you can have many UNIQUE constraints per table, but only
one PRIMARY KEY constraint per table.
SQL UNIQUE Constraint on CREATE TABLE
• The following SQL creates a UNIQUE constraint on the "ID" column
when the "Persons" table is created:
Check constraints
• The CHECK constraint is used to limit the value range that can be
placed in a column.
• If you define a CHECK constraint on a column it will allow only certain
values for this column.
• SQL CHECK on CREATE TABLE
Default constraints
• The DEFAULT constraint is used to set a default value for a column.
• The default value will be added to all new records, if no other value is
specified.
• SQL DEFAULT on CREATE TABLE
• To set a DEFAULT value for the “Location” column when the “Venue”
table is created –
• CREATE TABLE Venue ( ID int NOT NULL, Name varchar(255), Age int,
Location varchar(255) DEFAULT ‘Mumbai');
INSERT INTO Venue VALUES (4, 'Mira', 23, 'Delhi');
INSERT INTO Venue VALUES (5, 'Hema', 27);
INSERT INTO Venue VALUES (6, 'Neha', 25, 'Delhi');
INSERT INTO Venue VALUES (7, 'Khushi', 26);

• select * from Venue;


DDL COMMANDS
DDL (Data Definition Language):
• It is used to define the structure of the database .
• DDL actually consists of the SQL commands that can be used to define the database schema. (A
schema is a collection of database objects like tables, triggers, stored procedures, etc. )
• Data Definition Language, DDL, is the part of SQL that allows a database user to create and
restructure database objects, such as the creation or the deletion of a table.
• DDL commands :
• CREATE
• ALTER TABLE
• DROP TABLE
• TRUNCATE TABLE
• RENAME TABLE
• CREATE INDEX
• ALTER INDEX
• DROP INDEX
• CREATE VIEW
• DROP VIEW
CREATE, SHOW & DROP DATABASE
• The SQL CREATE DATABASE statement is used to create a new SQL database.
• Syntax:
• Example
• If you want to create a new database <college>,
• Posgres> CREATE DATABASE college;
• Once a database is created, you can check it in the list:
• Postgres> \list;

DROP DATABASE
• The SQL DROP DATABASE statement is used to drop an existing database in SQL schema.
• Syntax: DROP DATABASE DatabaseName;
• Example:
• SQL> DROP DATABASE college;
CREATE Table
• Creating a basic table involves naming the table and defining its columns and
each column's data type.
• Syntax :

Note:
• The data type of the columns may vary from one database to another.
• For example, NUMBER is supported in Oracle database for integer value whereas INT is supported in MySQL.
• Example:
• SQL> CREATE TABLE STUDENTS
(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS VARCHAR (25),
PRIMARY KEY (ID)
);
• You can see the structure of your
table by using disc command.
• Example: Postgres> \d STUDENTS;
DROP , TRUNCATE table command
• DROP:
• It is used to delete a table definition and all data from a table.
• This is very important to know that once a table is deleted all the
information available in the table is lost forever, so we have to be very
careful when using this command.
Syntax: DROP TABLE <table_name>;
• Truncate:
• It is used to delete all the rows from the table and free the containing
space.
Syntax: TRUNCATE TABLE <table_name>;
Alter table command
• syntax:
• To add new column:
• ALTER TABLE table_name ADD column_name datatype;
• To delete existing column:
• ALTER TABLE table_name DROP COLUMN column_name;
• To change column data type:
• ALTER TABLE table_name ALTER COLUMN column_name TYPE datatype;
• ALTER TABLE to ADD PRIMARY KEY constraint to a table
• ALTER TABLE table_name ADD CONSTRAINT MyPrimaryKey PRIMARY KEY
(column_name);
AGGRATION FUNCTIONS
AGGRATION FUNCTIONS:
• Aggregation functions are used to perform mathematical operations
on data values of a relation.
• take a collection (a set or multiset) of values as input and return a
single value.
• Some of the common aggregation functions used in SQL are:
• Average: avg
• Minimum: min
• Maximum: max
• Total: sum
• Count: count
Example
Table :
“Student”
• Count:
• Query: SELECT COUNT (PHONE) FROM STUDENT;
• Output: 8
• SUM:
• Query: SELECT SUM (AGE) FROM STUDENT;
• 163
• AVG:
• Query: SELECT AVG (AGE) FROM STUDENT;
• 20.37
• MAX:
• Query: SELECT MAX (AGE) FROM STUDENT;
• 25
• MIN:
• Query: SELECT MIN (AGE) FROM STUDENT;
• 18
Examples
1. Consider following relational tables:
1. Student (S_ID, Name, Dept_name)
2. Course ( course_ID, c_name, Dept_name)
3. Trainer (ID, name, dept_name)
4. Dept (Dept_ID, dept_name)
Solve above relation using DDL statements with primary key and
Foreign key.
NULL Value
• A field with a NULL value is a field with no value.
• If a field in a table is optional, it is possible to insert a new record or
update a record without adding a value to this field.
• Then, the field will be saved with a NULL value.
• It is not possible to test for NULL values with comparison operators,
such as =, <, or <>.
• We will have to use the IS NULL and IS NOT NULL operators instead.
DML COMMANDS
DML(Data Manipulation Language)
• Data Manipulation Language, DML, is the part of SQL used to
manipulate data within objects of a relational database.
• There are three basic DML commands:
• INSERT
• UPDATE
• DELETE
Data Manipulation commands
• A DML is a language that enables users to access or manipulate data
as organized by the appropriate data model.
• The types of access are:
• Retrieval of information stored in the database
• Insertion of new information into the database
• Deletion of information from the database
• Modification of information stored in the database
INSERT Command
• Insert statement is used to insert data into database tables.
• Syntax:
INSERT INTO <TABLE NAME> ( <COLUMNS TO INSERT> ) VALUES
( <VALUES TO INSERT> )
OR
INSERT INTO <TABLE NAME> VALUES
( <VALUES TO INSERT> )
• Insert into students (ID, NAME, AGE, ADDRESS)values
(1,’Rakesh’,20,’Pune’);
• Or
• Insert into students values (1,’Rakesh’,20,’Pune’);
• Output:
UPDATE command
• The SQL commands (UPDATE and DELETE) are used to modify the data that
is already in the database.
• `The SQL DELETE command uses a WHERE clause.
• SQL UPDATE statement is used to change the data of the records held by
tables.
• Which rows is to be update, it is decided by a condition. To specify
condition, we use WHERE clause.
• Syntax:
UPDATE table_name SET [column_name1= value1,... column_nameN = val
ueN] [WHERE condition]
Example:
• UPDATE students
• SET Name = ’Yogesh'
• WHERE Student_Id = ’5’
DELETE command
• DELETE:
• The DELETE statement is used to delete rows from a table.
• If you want to remove a specific row from a table you should use
WHERE condition.
• Syntax: DELETE FROM table_name [WHERE condition];
• OR
• DELETE FROM table_name;
• Example:
• Delete * from students;
• DELETE FROM students where age=“22”;
Complex Retrieval Queries
using Group By
GROUP BY and HAVING Clause
• The GROUP BY clause is a SQL command that is used to group rows
that have the same values.
• The GROUP BY clause is used in the SELECT statement.
• Optionally it is used in conjunction with aggregate functions to
produce summary reports from the database.
• That’s what it does, summarizing data from the database.
• The queries that contain the GROUP BY clause are called grouped
queries and only return a single row for every grouped item.
• Examples:
• Use GROUP BY on single column
• GROUP BY on multiple columns
• Use GROUP BY with ORDER BY
• GROUP BY with HAVING clause
• Use GROUP BY with JOINS
GROUP BY on single column

• Example:
• Find no. of employees per city.

• Query:
SELECT COUNT(EmpID), City
FROM Employees
GROUP BY City;
GROUP BY with ORDER BY
• When we use the SQL GROUP BY statement with the ORDER BY
clause, the values get sorted either in ascending or descending order.

• Example:
• Write a query to retrieve the number of employees in each city,
sorted in descending order.
SELECT COUNT(EmpID), City
FROM Employees
GROUP BY City
ORDER BY COUNT(EmpID) DESC;
GROUP BY with HAVING clause
• The SQL GROUP BY statement is used with ‘HAVING’ clause to mention conditions
on groups.
• Also, since we cannot use the aggregate functions with the WHERE clause, we
have to use the ‘HAVING’ clause to use the aggregate functions with GROUP BY.

• Example:
• Write a query to retrieve the number of employees in each city, having salary >
15000
SELECT COUNT(EmpID), City
FROM Employees
GROUP BY City
HAVING SALARY > 15000;
(Since all are records in the Employee table have a salary > 15000,
we will see the following table as output)
GROUP BY on multiple columns
• Example:
• Write a query to retrieve the number of employees having different
salaries in each city.

SELECT City, Salary, Count(*)


FROM Employees
GROUP BY City, Salary;
JOIN
• SQL Join statement is used to combine data or rows from two or
more tables based on a common field between them.
• Different types of Joins are as follows:
• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
INNER JOIN
• Returns records that have matching values in both tables.
• We can also write JOIN instead of INNER JOIN. JOIN is same as INNER
JOIN.
• This keyword will create the result-set by combining all rows from
both the tables where the condition satisfies
• i.e value of the common field will be the same.
• Syntax:
Student Course

SELECT Course.COURSE_ID, Student.NAME, Student.AGE FROM Student


INNER JOIN Course
ON Student.ROLL_NO = Course.ROLL_NO;
LEFT JOIN
• This join returns all the rows of the table on the left side of the join
and matches rows for the table on the right side of the join.
• For the rows for which there is no matching row on the right side, the
result-set will contain null.
• LEFT JOIN is also known as LEFT OUTER JOIN.
• Syntax:
Example
• SELECT Student.NAME,Course.COURSE_ID FROM Student
• LEFT JOIN Course
• ON Course.ROLL_NO = Student.ROLL_NO;
RIGHT JOIN
• This join returns all the rows of the table on the right side of the join
and matching rows for the table on the left side of the join.
• For the rows for which there is no matching row on the left side, the
result-set will contain null.
• RIGHT JOIN is also known as RIGHT OUTER JOIN.
• Syntax:
Example:
SELECT Student.NAME, Course.COURSE_ID
FROM Student
RIGHT JOIN Course
ON Course.ROLL_NO = Student.ROLL_NO;
FULL JOIN
• FULL JOIN creates the result-set by combining results of both LEFT
JOIN and RIGHT JOIN.
• The result-set will contain all the rows from both tables.
• For the rows for which there is no matching, the result-set will
contain NULL values.

• SELECT table1
Example:
SELECT Student.NAME, Course.COURSE_ID
FROM Student
FULL JOIN Course
ON Course.ROLL_NO = Student.ROLL_NO;
Example 2: full join
Logical Operators
LIKE condition
• LIKE condition is used to perform pattern matching to find the correct
result.
• It is used in SELECT, INSERT, UPDATE and DELETE statement with the
combination of WHERE clause.
• Syntax:
• expression LIKE pattern [ ESCAPE 'escape_character' ]
• Parameters
• expression: It specifies a column or field.
• pattern: It is a character expression that contains pattern matching.
• escape_character: It is optional. It allows you to test for literal instances of
a wildcard character such as % or _. If you do not provide the
escape_character, MySQL assumes that "\" is the escape_character.
Different LIKE operators with '%' and '_'
wildcards:
Examples
• 1) Using % (percent) Wildcard:

• 2) Using _ (Underscore) Wildcard:

• 3) using not like:


IN Condition
• The IN operator allows you to specify multiple values in a WHERE clause.
• The IN operator is a shorthand for multiple OR conditions.
Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);
OR
• SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
Examples
AND, OR and NOT Operators
• The WHERE clause can be combined with AND, OR,
and NOT operators.
• The AND and OR operators are used to filter records based on more
than one condition:
• The AND operator displays a record if all the conditions separated by AND are
TRUE.
• The OR operator displays a record if any of the conditions separated by OR is
TRUE.
• The NOT operator displays a record if the condition(s) is NOT TRUE.
BETWEEN Operator
• The BETWEEN operator selects values within a given range. The
values can be numbers, text, or dates.
• The BETWEEN operator is inclusive: begin and end values are
included.
• Syntax
• SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
Nested Queries in SQL
Subqueries
• Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE
statements along with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
• There are a few rules that subqueries must follow −
• Subqueries must be enclosed within parentheses.
• A subquery can have only one column in the SELECT clause, unless multiple
columns are in the main query for the subquery to compare its selected columns.
• An ORDER BY command cannot be used in a subquery, although the main query
can use an ORDER BY.
• Subqueries that return more than one row can only be used with multiple value
operators such as the IN operator.
• The BETWEEN operator cannot be used with a subquery. However, the BETWEEN
operator can be used within the subquery.
Nested
Queries
• In nested queries, a query is
written inside a query. The
result of inner query is used
in execution of outer query.
• Here we use STUDENT &
city tables for understanding
nested queries.
Subqueries with the
SELECT Statement

Retrieve id and city name of all students those


age is greater than 20.
Subquery with delete statement
• To delete record from employees whose works in ‘IT’ Department.
• Delete from employee where dept_id in(select department_id from
department where department_name=‘IT’;
Example 2:
Subqueries with the
UPDATE Statement
• The subquery can be used in conjunction with
the UPDATE statement.
• Either single or multiple columns in a table can
be updated when using a subquery with the
UPDATE statement.
• Syntax:
Example
• Consider the following employee database.
• Employee(emp_name, street,city,date_of_joining)
• Works(emp_name,company_name,salary)
• Company(company_name,city)
• Manages(emp_name,manager_name)
• Write SQL queries for following:
1. Modify the database so that ‘Deepa’lives in ‘Pune’;
2. Give all employees of ‘Aarya corporation’ a 10% rise in salary.
3. Display all employees who joined in the month of ‘March’;
4. Find all employees who earn more than average salary of all employees of their
company.
Nested Query:
• A nested query, also known as a subquery, is a query that is embedded within
another query.
• It is enclosed within parentheses and can appear in the SELECT, FROM, WHERE,
or HAVING clauses of an outer query.
• The result of a nested query is used as a condition or value in the outer query.
• Nested queries are commonly used to filter rows, perform calculations, or
retrieve data from related tables.
• Example:
• SELECT name FROM customers WHERE customer_id IN (SELECT DISTINCT customer_id FROM orders);

Slide 8- 89
Complex Query:
• A complex query is a query that involves multiple tables, joins, aggregations, or other complex
operations.
• It may include nested queries, but it can also include other elements such as JOINs, GROUP BY,
ORDER BY, and aggregate functions.
• The complexity of a query refers to the number of operations, the number of tables involved, and
the overall structure of the query.
• Complex queries are used to retrieve specific information from a database by combining multiple
tables and applying various conditions and aggregations.
• Example:
• SELECT p.product_id, p.product_name AS total_revenue FROM products p INNER JOIN order_details od
ON p.product_id = od.product_id;

Slide 8- 90
Examples
• Retrieve the names of all employees who work in departments located in New York.
• SELECT e.name FROM employee e JOIN department d ON e.department_id =
d.department_id WHERE d.location = 'New York';
• Find the names of all employees who have a salary higher than the average salary of
employees in their respective departments.
• SELECT e.name FROM employee e WHERE e.salary > ( SELECT AVG(salary) FROM employee
WHERE department_id = e.department_id );

Slide 8- 91
• List the names of all employees who have not been assigned to any
project.
• SELECT name FROM employee WHERE employee_id NOT IN ( SELECT
DISTINCT employee_id FROM project_assignment );
• Identify the names of all employees who have at least one
dependent.
• SELECT DISTINCT e.name FROM employee e JOIN dependent d ON
e.employee_id = d.employee_id;

Slide 8- 92
Examples

Slide 8- 93
• Select all employees who won an award.
• SELECT id, name FROM Employees WHERE id IN (SELECT employee_id FROM
Awards);
• Select all employees who never won an award.
• SELECT id, name FROM Employees WHERE id NOT IN (SELECT employee_id
FROM Awards);
• Select all Developers who earn more than all the Managers
• SELECT * FROM Employees WHERE role = 'Developer' AND salary > ALL (
SELECT salary FROM Employees WHERE role = 'Manager' );

Slide 8- 94
• Select all Developers who earn more than any Manager
• SELECT * FROM Employees WHERE role = 'Developer' AND salary > ANY (
SELECT salary FROM Employees WHERE role = 'Manager' );
• Explanation
• The developers with id 3 and 5 earn more than any manager:
• The developer with id 3 earns (30000) more than the manager with id 2 (10000)
• The developer with id 5 earns (50000) more than the managers with id 2 (10000) and 4 (40000)

Slide 8- 95
Co-related nested queries
• Select all employees whose salary is above the average salary of
employees in their role.
• SELECT * FROM Employees emp1 WHERE salary > ( SELECT AVG(salary) FROM
Employees emp2 WHERE emp1.role = emp2.role );
• Average salary of managers and developers using the below query:
• SELECT role, AVG(salary) FROM Employees GROUP BY role;

Slide 8- 96

You might also like