Introduction To SQL1 (CSE Department) 12.09.22
Introduction To SQL1 (CSE Department) 12.09.22
Introduction To SQL1 (CSE Department) 12.09.22
1
This is based on Textbook, its companion slide and other sources
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
Chapter Outline
2 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
History of SQL
• IBM Sequel language developed as part of System R project at the IBM San Jose
Research Laboratory
• Renamed Structured Query Language (SQL)
• A number of standards defined (for industry compatibility)
✓ SQL-86,SQL-89, SQL-92
✓ SQL:1999 (language name became Y2K compliant!)
✓ SQL:2003
• Commercial systems offer most, if not all, SQL-92 features, plus varying feature sets from
later standards and special proprietary features.
• Hence, not all examples here may work on your particular system.
3 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
4 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
The SQL data-definition language (DDL) allows the specification of information about relations,
including:
• The schema for each relation.
• The type of values associated with each attribute.
• The Integrity constraints (such as primary constraint and others)
• The set of indices to be maintained for each relation.
• Security and authorization information for each relation.
• The physical storage structure of each relation on disk.
5 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
6 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
7 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
8 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
9 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
10 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• drop table r : table r is dropped both its structure and data are lost
• alter table r add A D
✓ where A is the name of the attribute to be added to relation r and D is the domain of A.
✓ All exiting tuples in the relation are assigned null as the value for the new attribute.
11 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
12 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
The basic structure of an SQL query consists of three clauses: select, from, and where clause.
• Queries on a Single Relation.
select id,name, salary
from instructors
where dept_name='CSE' or dept_name='EEE';
13 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
14 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
15 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
16 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
Rename: Examples
1
as keyword is optional in oracle database
17 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
18 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
19 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
select E1.ID,
E1.Name, E1.BID
from Emp E1, Emp E2 • Such self-reference works like a for
where E1.BID=E2.ID;
loop in C.
Listing: Rename for self-reference
20 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
String Operations
select *
from depts
where name='CSE';
21 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
22 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• In practice, set operations are used to extract data from two relations having no
references (it may happen if data is collected from a legacy system and merged into new
system)
• It is also useful to merge data from different sources where conditions vary in each case.
• In many cases, (specially the SQL shown here) same SQL can be written alternatively
(using AND , OR, NOT operators) in much simpler way.
23 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
24 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
Null values
For employees who have null values for their bonus will return Null as total salary!!
• Solution: use nvl() built-in.
Example:
select Name, (salary+ nvl(bonus,0)) Total
from emp;
Now the null values found will be treated as 0, so total salary is now meaningful.
25 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
Aggregate Functions
• Aggregate functions are functions that take a collection of values as input and return a
single value. SQL offers five (5) standard built-in aggregate functions (Note: more are
available in many commercial databases):
✓ Average: avg
✓ Minimum: min
✓ Maximum: max
✓ Total: sum
✓ Count: count
Note: The input to sum and avg must be a collection of numbers, but the other operators
can operate on collections of nonnumeric data types, such as strings, as well
26 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• Find the total number of instructors who teach a course in the Spring 2018 semester
select count (distinct ID)
from teaches
where semester = 'Spring' and year = 2018;
27 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• Need to apply the aggregate function not only to a single set of tuples, but also to a group
of sets of tuples; we specify this in SQL using the group by clause.
• Tuples with the same value on all attributes in the group by clause are placed in one
group.
• Applying avg() of salary in instructor relation will return one single value. But we are more
interested for finding average salary for each department.
28 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
29 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
All selected attributes must be present either in (i) aggregate functions or in (ii) group by
clause.
• It is needed specially when we need joining over an aggregated function. For example:
Find the department name, its budget and total number of students. Trick: use either
max or min functions for those extra attributes.
test the Query
s e l e c t dname , max ( b u d g e t ) , c o u n t ( * ) t o t a l S t u d e n t
from depts , s t u d e n t s
w h e r e d e p t s . dname = s t u d e n t s . d e p t
g r o u p by dname ;
30 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• It is useful to state a condition that applies to groups (which will be computed at run-time)
• where clause can only be applied on the values already stored, but here the values are not
stored rather will be computed on-the-fly. Hence we have to use Having clause
• Find out the department name, its budget and total number of students for those who
have at-least 200 students;
select dname,max(budget), count (*) totalStudent
from depts,students
where depts.dname=students.dept
group by dept
having count(*)>=200;
• Note: predicates in the having clause are applied after the formation of groups whereas
predicates in the where clause are applied before forming groups
31 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
Sub-query
32 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• Find the employees name,address and salary for those who are now getting more than
the average salary of the company.
select id,name,salary
from emp
where salary>(select avg(salary) from emp);
• Here, subquery in the where part is first executed and the value is locked. This is
particularly important for update statement. Suppose, consider the Query:
Increase salary of employees by 20% for those who are now getting lower than the
average salary of the company.
update emp
set salary=salary*1.2
where salary<(select avg(salary) from emp);
33 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
34 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• A subquery can also be found in the FROM clause. These are called inline views.
• Example: Display the top five earner names and salaries from the EMPLOYEES table:
SELECT ROWNUM as RANK, last_name, salary
FROM (SELECT last_name, salary
FROM employees
ORDER BY salary DESC)
WHERE ROWNUM <= 5;
35 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
DML Statements
36 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
%Equivalently:
37 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• More dynamically, we might want to insert tuples on the basis of the result of a query.
• We can follow INSERT INTO T1 (SELECT ...FROM T2);
• Example: Make each student in the Music department who has earned more than 144
credit hours an instructor in the Music department with a salary of $18, 000 (fixed values
can be passed as well).
insert into instructor
select ID, name, dept_name, 18000
from student
where dept_name = 'Music' and total_cred > 144;
38 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• Give a 5% salary raise to those instructors who earn less than 70000
update instructor
set salary = salary * 1.05
where salary < 70000;
• Give a 5% salary raise to instructors whose salary is less than average (already shown!!)
update instructor
set salary = salary * 1.05
where salary < (select avg (salary)
from instructor);
39 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• Same Example: (using case) Increase salaries of instructors whose salary is over
$100, 000 by 3%, and all others by a 5%
update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end;
41 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
42 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
• Delete all instructors whose salary is less than the average salary of instructors
delete from instructor
where salary < (select avg (salary)
from instructor);
43 / 44
Overview of The SQL Query Language Basic Structure of SQL Queries The Rename Operation String Operations Set Operations Null values Aggregate Functions Nested Sub-query an
End of Chapter 3
Thank You
44 / 44