DBS UNIT II Notes
DBS UNIT II Notes
Assistant Professor
Department of Computer Applications
M S Ramaiah Institute of Technology
[email protected]
Ph:9900087291
Contents
1 Basic Retrieval Queries in SQL 2
1.1 The SELECT – FROM – WHERE structure of basic SQL Queries 2
2 Practice Sessions 4
2.1 Database and Queries . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Solutions :Creating the tables and inserting the data . . . . . . . 11
2.3 Solutions for Queries . . . . . . . . . . . . . . . . . . . . . . . . . 13
3 Complex Queries 30
3.1 Nested Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.2 Other Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.3 Ambiguity of attribute names . . . . . . . . . . . . . . . . . . . . 32
3.4 Correlated nested queries . . . . . . . . . . . . . . . . . . . . . . 32
3.5 Aggregate Functions . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.6 Grouping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.7 The Having-Clause . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.8 Where.....Having . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5 Stored Procedures 46
5.1 Creating a Procedure . . . . . . . . . . . . . . . . . . . . . . . . . 46
5.2 Executing a Standalone Procedure . . . . . . . . . . . . . . . . . 47
5.3 Deleting a Standalone Procedure . . . . . . . . . . . . . . . . . . 47
6 PL/SQL Function 48
6.1 Creating a Function . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.2 Calling a Function . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.3 Specifying Constraints as Assertions . . . . . . . . . . . . . . . . 50
7 SQL Triggers 50
7.1 Trigger syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Page 1
Database Systems - Unit II Nithya BN
UNIT II
SQL Basic Retrieval Queries in SQL; INSERT, DELETE and UPDATE state-
ments in SQL; Additional features of SQL, Complex Queries Pl/SQLIntroduction
to PL/SQL, Procedures and Functions, Triggers.
• Retrieve the birth date and address of the employee(s) whose name is
‘John B. Smith’
Page 2
Database Systems - Unit II Nithya BN
• Retrieve the name and address of all employees who work for the ‘Re-
search’ department
• For every project located in ‘Stafford’, list the project number, the con-
trolling department number, and the department manager’s last name,
address and birth date
• In SQL, the same name can be used for two (or more) attributes as long as
the attributes are in different tables. In this case, we must specify which
attribute you are referring to from what table which addressing same name
attributes of different tables as under.
Page 3
Database Systems - Unit II Nithya BN
2 Practice Sessions
2.1 Database and Queries
Create the following database schema and write queries.
DEPARTMENT (dept id (int), dept name (varchar (20)), dep location (varchar
(15)))
SALARY-GRADE (grade (int), min sal(int), max sal(int))
EMPLOYEES (emp id(int), emp name (varchar (15)), job name (varchar (10)),
manager id(int), hire date(date), salary (float (10,2)), commission (float (7,2)),
dept id(int))
Page 4
Database Systems - Unit II Nithya BN
9. Write a query in SQL to list the emp id,salary, and commission of all the
employees.
10. Write a query in SQL to display the unique department with jobs.
11. Write a query in SQL to list the employees who does not belong to de-
partment 2001.
12. Write a query in SQL to list the employees who joined before 1991.
13. Write a query in SQL to display the average salaries of all the employees
who works as ANALYST.
14. Write a query in SQL to display the details of the employee BLAZE.
15. Write a query in SQL to display all the details of the employees whose
commission is more than their salary.
16. Write a query in SQL to list the employees whose salary is more than 3000
after giving 25% increment.
Page 5
Database Systems - Unit II Nithya BN
17. Write a query in SQL to list the name of the employees, those having six
characters to their name.
18. Write a query in SQL to list the employees who joined in the month
January.
19. Write a query in SQL to list the name of employees and their manager
separated by the string ’works for’.
20. Write a query in SQL to list all the employees whose designation is CLERK.
21. Write a query in SQL to list the employees whose experience is more than
27 years.
22. Write a query in SQL to list the employees whose salaries are less than
3500.
23. Write a query in SQL to list the name, job name, and salary of any em-
ployee whose designation is ANALYST.
24. Write a query in SQL to list the employees who have joined in the year
1991.
25. Write a query in SQL to list the name, id, hire date, and salary of all the
employees joined before 1 apr 91.
26. Write a query in SQL to list the employee name, and job name who are
not working under a manager.
27. Write a query in SQL to list all the employees joined on 1st may 91.
28. Write a query in SQL to list the id, name, salary, and experiences of all
the employees working for the manger 68319.
29. Write a query in SQL to list the id, name, salary, and experience of all
the employees who earn more than 100 as daily salary.
30. Write a query in SQL to list the employees who are retiring after 31-Dec-99
after completion of 8 years of service period.
31. Write a query in SQL to list those employees whose salary is an odd value.
32. Write a query in SQL to list those employees whose salary contain only 3
digits. 33. Write a query in SQL to list the employees who joined in the
month of APRIL.
33. Write a query in SQL to list the employees those who joined in company
before 19th of the month.
34. List the employees who are SALESMAN and gathered an experience which
month portion is more than 10.
35. Write a query in SQL to list the employees of department id 3001 or 1001
joined in the year 1991.
36. Write a query in SQL to list the employees of department id 3001 or 1001
joined in the year 1991.
Page 6
Database Systems - Unit II Nithya BN
37. Write a query in SQL to list all the employees of designation CLERK in
department no 2001.
38. Write a query in SQL to list the ID, name, salary, and job name of the
employees for
• Annual salary is below 34000 but receiving some commission which
should not be more than the salary,
• And designation is SALESMAN and working for department 3001.
39. Write a query in SQL to list the employees who are either CLERK or
MANAGER
40. Write a query in SQL to list the employees who joined in any year except
the month February.
41. Write a query in SQL to list the employees who joined in the year 91.
42. Write a query in SQL to list the employees who joined in the month of
June in 1991.
43. Write a query in SQL to list the employees whose annual salary is within
the range 24000 and 50000.
44. Write a query in SQL to list the employees who have joined on the follow-
ing dates 1st May,20th Feb, and 03rd Dec in the year 1991.
45. Write a query in SQL to list the employees working under the managers
63679,68319,66564,69000.
46. Write a query in SQL to list the employees who joined after the month
JUNE in the year 1991.
47. Write a query in SQL to list the employees who joined in 90’s.
48. Write a query in SQL to list the managers of department 1001 or 2001.
49. Write a query in SQL to list the employees, joined in the month FEBRU-
ARY with a salary range between 1001 to 2000.
50. Write a query in SQL to list all the employees who joined before or after
1991.
51. Write a query in SQL to list the employees along with department name.
52. Write a query in SQL to list the name, job name, annual salary, depart-
ment id, department name and grade of the employees who earn 60000 in
a year or not working as an ANALYST.
53. Write a query in SQL to list the name, job name, manager id, salary, man-
ager name, manager’s salary for those employees whose salary is greater
than the salary of their managers.
54. Write a query in SQL to list the employees name, department, salary
and commission. For those whose salary is between 2000 and 5000 while
location is PERTH.
Page 7
Database Systems - Unit II Nithya BN
55. Write a query in SQL to list the grade, employee name for the department
id 1001 or 3001 but salary grade is not 4 while they joined the company
before 1992-12-31.
56. Write a query in SQL to list the employees whose manager name is
JONAS.
57. Write a query in SQL to list the name and salary of FRANK if his salary
is equal to max sal of his grade.
58. Write a query in SQL to list the employees who are working either MAN-
AGER or ANALYST with a salary range between 2000 to 5000 without
any commission.
59. Write a query in SQL to list the id, name, salary, and location of the
employees working at PERTH,or MELBOURNE with an experience over
10 years.
60. Write a query in SQL to list the employees along with their location who
belongs to SYDNEY, MELBOURNE with a salary range between 2000
and 5000 and joined in 1991.
61. Write a query in SQL to list the employees with their location and grade
for MARKETING department who comes from MELBOURNE or PERTH
within the grade 3 to 5 and experience over 5 years.
62. Write a query in SQL to list the employees who are senior to their own
manager.
63. Write a query in SQL to list the employee with their grade for the grade
4.
64. Write a query in SQL to list the employees in department PRODUCTION
or AUDIT who joined after 1991 and they are not MARKER or ADELYN
to their name.
65. Write a query in SQL to list the employees in the ascending order of their
salaries.
66. Write a query in SQL to list the details of the employees in ascending
order to the department id and descending order to the jobs.
67. Write a query in SQL to display all the unique job in descending order.
68. Write a query in SQL to list the id, name, monthly salary, daily salary of
all the employees in the ascending order of their annual salary.
69. Write a query in SQL to list the employees in descending order who are
either ’CLERK’ or ’ANALYST’.
Page 8
Database Systems - Unit II Nithya BN
72. Write a query in SQL to list the employees who are drawing the salary
less than 1000 and sort the output in ascending order on salary.
73. Write a query in SQL to list the details of the employees in ascending
order on the salary.
74. Write a query in SQL to list the employees in ascending order on job name
and descending order on employee id.
75. Write a query in SQL to list the unique jobs of department 2001 and 3001
in descending order.
76. Write a query in SQL to list all the employees except PRESIDENT and
MANAGER in ascending order of salaries.
77. Write a query in SQL to list the employees in ascending order of the salary
whose annual salary is below 25000.
78. Write a query in SQL to list the employee id, name, annual salary, daily
salary of all the employees in the ascending order of annual salary who
works as a SALESMAN.
79. Write a query in SQL to list the employee id, name, hire date, current date
and experience of the employees in ascending order on their experiences.
80. Write a query in SQL to list the employees in ascending order of designa-
tions of those, joined after the second half of 1991.
81. Write a query in SQL to list the total information of employees table
along with department, and location of all the employees working under
FINANCE and AUDIT in the ascending department no.
82. Write a query in SQL to display the total information of the employees
along with grades in ascending order.
83. Write a query in SQL to list the name, job name, department, salary, and
grade of the employees according to the department in ascending order.
84. Write a query in SQL to list the name, job name, salary, grade and de-
partment name of employees except CLERK and sort result set on the
basis of highest salary.
85. Write a query in SQL to list the employee ID, name, salary, department,
grade, experience, and annual salary of employees working for department
1001 or 2001.
86. Write a query in SQL to list the details of the employees along with the
details of their departments.
87. Write a query in SQL to list the employees who are senior to their own
MANAGERS.
88. Write a query in SQL to list the employee id, name, salary, and depart-
ment id of the employees in ascending order of salary who works in the
department 1001.
Page 9
Database Systems - Unit II Nithya BN
89. Write a query in SQL to find the highest salary from all the employees.
90. Write a query in SQL to find the average salary and average total remu-
neration (salary and commission) for each type of job.
91. Write a query in SQL to find the total annual salary distributed against
each job in the year 1991.
92. Write a query in SQL to list the employee id, name, department id, loca-
tion of all the employees.
93. Write a query in SQL to list the employee id, name, location, department
of all the departments 1001 and 2001.
94. Write a query in SQL to list the employee id, name, salary, grade of all
the departments 1001 and 2001.
95. Write a query in SQL to list the manager no and the number of employees
working for those managers in ascending order on manager id.
96. Write a query in SQL to display the number of employee for each job in
each department.
97. Write a query in SQL to list the department where at least two employees
are working.
98. Write a query in SQL to display the Grade, Number of employees, and
maximum salary of each grade.
99. Write a query in SQL to display the department name, grade, no. of
employees where at least two employees are working as a SALESMAN.
100. Write a query in SQL to list the no. of employees in each department
where the no. is less than 4.
101. Write a query in SQL to list the name of departments where atleast 2
employees are working in that department.
102. Write a query in SQL to check whether all the employee’s numbers are
indeed unique.
103. Write a query in SQL to list the no. of employees and average salary
within each department for each job name
104. Write a query in SQL to list the names of those employees starting with
’A’ and with six characters in length.
105. Write a query in SQL to list the employees whose name is six characters
in length and third character must be ’R’.
106. Write a query in SQL to list the name of the employee of six characters
long and starting with ’A’ and ending with ’N’.
107. Write a query in SQL to list the employees who joined in the month of
which second character is ’a’.
Page 10
Database Systems - Unit II Nithya BN
108. Write a query in SQL to list the employees whose names containing the
character set ’AR’ together.
109. Write a query in SQL to list the employees those who joined in 90’s.
110. Write a query in SQL to list the employees whose ID not starting with
digit 68.
111. Write a query in SQL to list the employees whose names containing the
letter ’A’.
112. Write a query in SQL to list the employees whose name is ending with ’S’
and six characters long.
113. Write a query in SQL to list the employees who joined in the month having
char ’A’ at any position.
114. Write a query in SQL to list the employees who joined in the month having
second char is ’A’.
Page 11
Database Systems - Unit II Nithya BN
max_sal int,
primary key(grade)
);
Page 12
Database Systems - Unit II Nithya BN
,66928,’28-Sep-1991’,1350.00,1500.00,3001);
insert into emp_bn values(68454,’Tucker’,’Salesman’
,66928,’08-Sep-1991’,1600.00,0.00,3001);
insert into emp_bn values(68736,’Adnres’,’Clerk’
,67858,’23-May-1997’,1200.00,null,2001);
insert into emp_bn values(69000,’Julius’,’Clerk’
,66928,’03-Dec-1991’,1050.00,null,3001);
insert into emp_bn values(69324,’Marker’,’Clerk’
,67832,’23-Jan-1992’,1400.00,null,1001);
3. Write a query in SQL to display the unique designations for the employees.
4. Write a query in SQL to list the emp name and salary is increased by 15%
and expressed as no.of Dollars.
6. Write a query in SQL to list the employees with Hire date in the format
like February 22, 1991.
Page 13
Database Systems - Unit II Nithya BN
8. Write a query in SQL to list the emp id,salary, and commission of all the
employees.
10. Write a query in SQL to list the employees who does not belong to de-
partment 2001.
11. Write a query in SQL to list the employees who joined before 1991.
12. Write a query in SQL to display the average salaries of all the employees
who works as ANALYST.
13. Write a query in SQL to display the details of the employee BLAZE.
14. Write a query in SQL to display all the details of the employees whose
commission is more than their salary.
15. Write a query in SQL to list the employees whose salary is more than 3000
after giving 25% increment.
Page 14
Database Systems - Unit II Nithya BN
16. Write a query in SQL to list the name of the employees, those having six
characters to their name.
17. Write a query in SQL to list the employees who joined in the month
January.
18. Write a query in SQL to list the name of employees and their manager
separated by the string ’works for’.
19. Write a query in SQL to list all the employees whose designation is CLERK.
20. Write a query in SQL to list the employee name and his experience in
days.
21. Write a query in SQL to extract year, month and date separately from
the column hire date.
OR
OR
Page 15
Database Systems - Unit II Nithya BN
22. Write a query in SQL to list the employees whose salaries are less than
3500.
23. Write a query in SQL to list the name, job name, and salary of any em-
ployee whose designation is ANALYST.
SELECT ename,job_name,salary
FROM emp_bn
WHERE job_name = ’Analyst’;
24. Write a query in SQL to list the employees who have joined in the year
1991.
25. Write a query in SQL to list the name, id, hire date, and salary of all the
employees joined before 1 apr 91.
SELECT e.emp_id,e.ename,e.hire_date,e.salary
FROM emp_bn e WHERE hire_date <’01-Apr-1991’;
26. Write a query in SQL to list the employee name, and job name who are
not working under a manager.
27. Write a query in SQL to list all the employees joined on 1st may 91.
28. Write a query in SQL to list the id, name, salary, and experience of all
the employees who earn more than 100 as daily salary.
29. Write a query in SQL to list those employees whose salary is an odd value.
Page 16
Database Systems - Unit II Nithya BN
30. Write a query in SQL to list those employees whose salary contain only 3
digits.
31. Write a query in SQL to list the employees who joined in the month of
APRIL.
32. List the employees who are SALESMAN and gathered an experience which
month portion is more than 10.
33. Write a query in SQL to list the employees of department id 3001 or 1001
joined in the year 1991.
34. Write a query in SQL to list all the employees of designation CLERK in
department no 2001.
35. Write a query in SQL to list the ID, name, salary, and job name of the
employees for
SELECT emp_id,ename,salary,job_name
FROM emp_bn WHERE 12*(salary+commission) < 34000
AND commission IS NOT NULL AND commission < salary
AND job_name = ’Salesman’
AND dept_id = 3001;
Page 17
Database Systems - Unit II Nithya BN
36. Write a query in SQL to list the employees who are either CLERK or
MANAGER
37. Write a query in SQL to list the employees who joined in any year except
the month February.
38. Write a query in SQL to list the employees who joined in the year 91.
39. Write a query in SQL to list the employees who joined in the month of
June in 1991.
40. Write a query in SQL to list the employees whose annual salary is within
the range 24000 and 50000.
41. Write a query in SQL to list the employees who have joined on the follow-
ing dates 1st May,20th Feb, and 03rd Dec in the year 1991.
42. Write a query in SQL to list the employees working under the managers
63679,68319,66564,69000.
Page 18
Database Systems - Unit II Nithya BN
43. Write a query in SQL to list the employees who joined after the month
JUNE in the year 1991.
44. Write a query in SQL to list the employees who joined in 90’s.
45. Write a query in SQL to list the managers of department 1001 or 2001.
46. Write a query in SQL to list the employees, joined in the month FEBRU-
ARY with a salary range between 1001 to 2000.
47. Write a query in SQL to list all the employees who joined before or after
1991.
48. Write a query in SQL to list the employees along with department name.
Page 19
Database Systems - Unit II Nithya BN
49. Write a query in SQL to list the name, job name, annual salary, depart-
ment id, department name and grade of the employees who earn 60000 in
a year or not working as an ANALYST.
SELECT e.ename,e.job_name,(12*e.salary)
"Annual Salary",e.dept_id,d.dept_name,s.grade
FROM emp_bn e,dept_bn d, salary_grade_bn s
WHERE e.dept_id = d.dept_id
AND e.salary BETWEEN s.min_sal
AND s.max_sal AND (((12*e.salary)>= 60000)
OR (e.job_name != ’ANALYST’));
50. Write a query in SQL to list the name, job name, manager id, salary, man-
ager name, manager’s salary for those employees whose salary is greater
than the salary of their managers.
51. Write a query in SQL to list the employees name, department, salary
and commission. For those whose salary is between 2000 and 5000 while
location is PERTH.
52. Write a query in SQL to list the grade, employee name for the department
id 1001 or 3001 but salary grade is not 4 while they joined the company
before 1992-12-31.
Page 20
Database Systems - Unit II Nithya BN
53. Write a query in SQL to list the employees whose manager name is
JONAS.
54. Write a query in SQL to list the name and salary of FRANK if his salary
is equal to max sal of his grade.
SELECT e.ename,e.salary
FROM emp_bn e, salary_grade_bn s
WHERE e.ename = ’Frank’
AND e.salary BETWEEN s.min_sal
AND s.max_sal AND e.salary = s.max_sal ;
55. Write a query in SQL to list the employees who are working either MAN-
AGER or ANALYST with a salary range between 2000 to 5000 without
any commission.
56. Write a query in SQL to list the id, name, salary, and location of the
employees working at PERTH,or MELBOURNE with an experience over
10 years.
57. Write a query in SQL to list the employees along with their location who
belongs to SYDNEY, MELBOURNE with a salary range between 2000
and 5000 and joined in 1991.
Page 21
Database Systems - Unit II Nithya BN
58. Write a query in SQL to list the employees with their location and grade
for MARKETING department who comes from MELBOURNE or PERTH
within the grade 3 to 5 and experience over 5 years.
59. Write a query in SQL to list the employees who are senior to their own
manager.
60. Write a query in SQL to list the employee with their grade for the grade
4.
62. Write a query in SQL to list the employees in the ascending order of their
salaries.
Page 22
Database Systems - Unit II Nithya BN
63. Write a query in SQL to list the details of the employees in ascending
order to the department id and descending order to the jobs.
64. Write a query in SQL to display all the unique job in descending order.
65. Write a query in SQL to list the id, name, monthly salary, daily salary of
all the employees in the ascending order of their annual salary.
66. Write a query in SQL to list the employees in descending order who are
either ’CLERK’ or ’ANALYST’.
68. Write a query in SQL to list the employees in ascending order of seniority
who joined on 1-MAY-91,or 3-DEC-91, or 19-JAN-90.
69. Write a query in SQL to list the employees who are drawing the salary
less than 1000 and sort the output in ascending order on salary.
70. Write a query in SQL to list the details of the employees in ascending
order on the salary.
Page 23
Database Systems - Unit II Nithya BN
71. Write a query in SQL to list the employees in ascending order on job name
and descending order on employee id.
72. Write a query in SQL to list the unique jobs of department 2001 and 3001
in descending order.
73. Write a query in SQL to list all the employees except PRESIDENT and
MANAGER in ascending order of salaries.
74. Write a query in SQL to list the employees in ascending order of the salary
whose annual salary is below 25000.
75. Write a query in SQL to list the employee id, name, annual salary, daily
salary of all the employees in the ascending order of annual salary who
works as a SALESMAN
SELECT e.emp_id,e.ename,12*salary
"Annual Salary",floor((12*salary)/365)
"Daily Salary" FROM emp_bn e
WHERE e.job_name = ’Salesman’
ORDER BY "Annual Salary" ASC;
OR
SELECT e.emp_id,e.ename,12*salary "Annual Salary",
(12*salary)/365 "Daily Salary"
FROM emp_bn e WHERE e.job_name = ’Salesman’
ORDER BY "Annual Salary" ASC;
Page 24
Database Systems - Unit II Nithya BN
76. Write a query in SQL to list the employee id, name, hire date, current date
and experience of the employees in ascending order on their experiences.
77. Write a query in SQL to list the employees in ascending order of designa-
tions of those, joined after the second half of 1991.
78. Write a query in SQL to list the total information of employees table
along with department, and location of all the employees working under
FINANCE and AUDIT in the ascending department no.
79. Write a query in SQL to display the total information of the employees
along with grades in ascending order.
80. Write a query in SQL to list the name, job name, department, salary, and
grade of the employees according to the department in ascending order.
Page 25
Database Systems - Unit II Nithya BN
81. Write a query in SQL to list the name, job name, salary, grade and de-
partment name of employees except CLERK and sort result set on the
basis of highest salary.
82. Write a query in SQL to list the employee ID, name, salary, department,
grade, experience, and annual salary of employees working for department
1001 or 2001.
83. Write a query in SQL to list the details of the employees along with the
details of their departments.
84. Write a query in SQL to list the employees who are senior to their own
MANAGERS.
85. Write a query in SQL to list the employee id, name, salary, and depart-
ment id of the employees in ascending order of salary who works in the
department 1001.
Page 26
Database Systems - Unit II Nithya BN
86. Write a query in SQL to find the highest salary from all the employees.
87. Write a query in SQL to find the average salary and average total remu-
neration (salary and commission) for each type of job.
88. Write a query in SQL to find the total annual salary distributed against
each job in the year 1991.
89. Write a query in SQL to list the employee id, name, department id, loca-
tion of all the employees.
90. Write a query in SQL to list the employee id, name, location, department
of all the departments 1001 and 2001.
91. List the employee id, name, salary, grade of all the employees
92. Write a query in SQL to list the manager no and the number of employees
working for those managers in ascending order on manager id.
Page 27
Database Systems - Unit II Nithya BN
93. Write a query in SQL to display the number of employee for each job in
each department.
94. Write a query in SQL to list the department where at least two employees
are working.
95. Write a query in SQL to display the Grade, Number of employees, and
maximum salary of each grade.
96. Write a query in SQL to display the department name, grade, no. of
employees where at least two employees are working as a SALESMAN.
97. Write a query in SQL to list the no. of employees in each department
where the no. is less than 4.
98. Write a query in SQL to list the name of departments where atleast 2
employees are working in that department.
Page 28
Database Systems - Unit II Nithya BN
99. Write a query in SQL to check whether all the employee’s numbers are
indeed unique.
100. Write a query in SQL to list the no. of employees and average salary
within each department for each job name
101. Write a query in SQL to list the names of those employees starting with
’A’ and with six characters in length.
102. Write a query in SQL to list the employees whose name is six characters
in length and third character must be ’R’.
103. Write a query in SQL to list the name of the employee of six characters
long and starting with ’A’ and ending with ’N’.
104. Write a query in SQL to list the employees who joined in the month of
which second character is ’a’.
105. Write a query in SQL to list the employees whose names containing the
character set ’AR’ together.
106. Write a query in SQL to list the employees those who joined in 90’s.
Page 29
Database Systems - Unit II Nithya BN
107. Write a query in SQL to list the employees whose ID not starting with
digit 68.
108. Write a query in SQL to list the employees whose names containing the
letter ’A’.
109. Write a query in SQL to list the employees whose name is ending with ’S’
and six characters long.
110. Write a query in SQL to list the employees who joined in the month having
char ’A’ at any position.
111. Write a query in SQL to list the employees who joined in the month having
second char is ’A’.
3 Complex Queries
3.1 Nested Queries
• Some queries require that existing values in the database be fetched and
then used in a comparison condition.
• Such queries can be conveniently formulated by using nested queries, which
are complete select-from-where blocks within the WHERE clause of an-
other query.
Page 30
Database Systems - Unit II Nithya BN
1. Retrieve the name and address of all employees who work for the ’Re-
search’ department.
2. Make a list of all project numbers for projects that involve an employee
whose last name is ’Smith’ either as a worker or as a manager of the
department that controls the project.
3. Retrieve the Ssns of all employees who work on the same (project,hours)
combination on some project that employee (whose Ssn is equal to ‘33344555’)
works on.
Page 31
Database Systems - Unit II Nithya BN
1. Retrieve the names of employees whose salary is greater than the salary
of all the employees in department 5
• The result of a correlated nested query is different for each tuple (or com-
bination of tuples) of the relation(s) the outer query.
1. Retrieve the name of each employee who has a dependent with the same
first name as the employee.
Page 32
Database Systems - Unit II Nithya BN
The CONTAINS operator compares two sets (or multi sets) of values, and
returns TRUE if one set contains all values in the other set (reminiscent
of the division operation of algebra).
3. Retrieve the name of each employee who works on all the projects con-
trolled by department number 5.
Page 33
Database Systems - Unit II Nithya BN
7. Retrieve the name of each employee who works on all the projects con-
trolled by department number 5
Explicit sets
• It is also possible to use an explicit (enumerated) set of values in the
WHERE-clause rather than a nested query
8. Retrieve the social security numbers of all employees who work on project
number 1, 2, or 3.
• Find the sum of the salaries of all employees, the maximum salary, the
minimum salary, and the average salary among all employees.
Page 34
Database Systems - Unit II Nithya BN
• Find the maximum salary, the minimum salary, and the average salary
among employees who work for the ’Research’ department.
– The asterisk (*) refers to the rows (tuples), so COUNT (*) returns
the number of rows in the result of the query
– COUNT function can be used to count values in a column rather
than tuples
Page 35
Database Systems - Unit II Nithya BN
• Retrieve the names of all employees who have two or more dependents
The correlated nested query counts the number of dependents that each
employee has; if this is greater than or equal to two, the employee tuple
is selected.
3.6 Grouping
• Grouping is used to create subgroups of tuples before summarization.
Page 36
Database Systems - Unit II Nithya BN
Page 37
Database Systems - Unit II Nithya BN
3.8 Where.....Having
• Selection conditions in the WHERE clause limit the tuples to which func-
tions are applied
• The HAVING clause serves to choose whole groups
Page 38
Database Systems - Unit II Nithya BN
• For each project, retrieve the project number, the project name, and the
number of employees from department 5 who work on the project.
• For each department that has more than five employees, retrieve the de-
partment number and the number of its employees who are making more
than $40,000.
Page 39
Database Systems - Unit II Nithya BN
– Header Section
Page 40
Database Systems - Unit II Nithya BN
– Header Section
– Header Section
– Executable Section
– Exception Section
• This syntax defines a variable of the type of the referenced column on the
referenced table
Page 41
Database Systems - Unit II Nithya BN
DECLARE
salary number(6);
WHERE "salary" is a variable of datatype number and of length 6.
• When a variable is specified as NOT NULL, you must initialize the variable
when it is declared.
DECLARE
salary number(6);
dept varchar2(10) NOT NULL := "HR Dept";
Page 42
Database Systems - Unit II Nithya BN
Example 1. The below program will get the salary of an employee with ssn
‘888665555’ and display it on the screen.
DECLARE
var_salary number(10,2);
var_ssn char(9):= ’888665555’;
BEGIN
SELECT salary INTO var_salary
FROM employee
WHERE ssn=var_ssn;
dbms_output.put_line(var_salary);
dbms_output.put_line(var_ssn);
dbms_output.put_line(’The employee ’
|| var_ssn || ’ has salary ’ || var_salary);
END;
/
Valid
DECLARE salary increase CONSTANT number (3) := 10;
Invalid
DECLARE
salary_increase CONSTANT number(3);
BEGIN
salary_increase := 100;
dbms_output.put_line (salary_increase);
END;
/
Page 43
Database Systems - Unit II Nithya BN
DataType Description
Numeric Numeric Values on which arithmetic operations are performed.
Character Alphanumeric values that represent single characters or strings of characters
Boolean Logical values on which logical operations are performed
Datetinme Dates and Times
• PL/SQL provides subtypes of data types. For example, the data type
NUMBER has a subtype called INTEGER.
Example 2. Example
DECLARE
num1 INTEGER;
num2 REAL;
num3 DOUBLE PRECISION;
BEGIN
null;
END;
/
Page 44
Database Systems - Unit II Nithya BN
• IF....THEN...ELSE....END IF;
• IF....THEN...ELSIF....THEN....ELSE....END IF;
• The CASE statement selects one sequence of statements to execute.
• However, to select the sequence, the CASE statement uses a selector rather
than multiple Boolean expressions.
• A selector is an expression, whose value is used to select one of several
alternatives.
Page 45
Database Systems - Unit II Nithya BN
LOOP
...SQL Statements...
EXIT;
END LOOP;
WHILE loops
WHILE condition LOOP
...SQL Statements...
END LOOP;
FOR loops
5 Stored Procedures
• A stored procedure or in simple a proc is a named PL/SQL block which
performs one or more specific task. This is similar to a procedure in other
programming languages.
Page 46
Database Systems - Unit II Nithya BN
– The optional parameter list contains name, mode and types of the
parameters. IN represents that value will be passed from outside and
OUT represents that this parameter will be used to return a value
outside of the procedure.
– procedure-body contains the executable part.
– The AS keyword is used instead of the IS keyword for creating a
standalone procedure.
The above procedure named ’greetings’ can be called with the EXECUTE key-
word as:
EXECUTE greetings;
The above call would display:
Hello World
PL/SQL procedure successfully completed.
The procedure can also be called from another PL/SQL block:
BEGIN greetings;
END;
/
The above call would display:
Hello World
PL/SQL procedure successfully completed.
Page 47
Database Systems - Unit II Nithya BN
Example 2.
BEGIN
DROP PROCEDURE greetings;
END; /
6 PL/SQL Function
• A PL/SQL function is same as a procedure except that it returns a value.
Page 48
Database Systems - Unit II Nithya BN
DECLARE c number(2);
BEGIN c := totalCustomers();
dbms_output.put_line(’Total no. of Customers: ’ || c);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Page 49
Database Systems - Unit II Nithya BN
RETURN z;
END;
BEGIN a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(’ Maximum of (23,45): ’ || c);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Example 3.
CREATE ASSERTION
SALARY_CONSTRAINT
CHECK (NOT EXISTS (SELECT *
FROM EMPLOYEE E, EMPLOYEE M,
DEPARTMENT D
WHERE E.Salary>M.Salary
AND E.Dno=D.Dnumber
AND D.Mgr_ssn=M.Ssn ) );
7 SQL Triggers
• Objective: to monitor a database and take action when a condition
occurs
• A typical trigger has three components
– Event
– Condition
Page 50
Database Systems - Unit II Nithya BN
Page 51
Database Systems - Unit II Nithya BN
Example 3.
• the Total sal attribute is a derived attribute, whose value should be the
sum of the salaries of all employees who are assigned to the particular
department.
• Maintaining the correct value of such a derived attribute can be done via
an active rule.
Page 52