DBMS - Module - 3 Part 2
DBMS - Module - 3 Part 2
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
• Interactive Language –
• Easy to learn and understand, answers to complex queries can be received in
seconds.
• 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.
• 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
• 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);
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 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:
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