SlideShare a Scribd company logo
1 of 26
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-1
Summary of SQL Queries
 A query in SQL can consist of up to six clauses, but only
the first two, SELECT and FROM, are mandatory. The
clauses are specified in the following order:
SELECT <attribute list>
FROM <table list>
[WHERE <condition>]
[GROUP BY <grouping attribute(s)>]
[HAVING <group condition>]
[ORDER BY <attribute list>]
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-2
Summary of SQL Queries
(cont.)
 The SELECT-clause lists the attributes or functions to be
retrieved
 The FROM-clause specifies all relations (or aliases) needed in
the query but not those needed in nested queries
 The WHERE-clause specifies the conditions for selection and
join of tuples from the relations specified in the FROM-clause
 GROUP BY specifies grouping attributes
 HAVING specifies a condition for selection of groups
 ORDER BY specifies an order for displaying the result of a
query
 A query is evaluated by first applying the WHERE-clause, then
GROUP BY and HAVING, and finally the SELECT-clause
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-3
THE HAVING-CLAUSE
 Query: For each project on which more than two
employees work , retrieve the project number, project
name, and the number of employees who work on that
project.
SELECT PNUMBER, PNAME, COUNT (*)
FROM PROJECT, WORKS_ON
WHERE PNUMBER=PNO
GROUP BY PNUMBER, PNAME
HAVING COUNT (*) > 2
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-4
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-5
SQL> select * from employee;
NAME SSN SALARY DNO SUPERSSN
----- ---------- ---------- ---------- ----------
johny 12345 40000 5 33344
frank 33344 30000 5 88866
james 88866 40000 4 77777
borgs 77777 30000 5 88866
(Zhang 55555 35000 4 12345
TTTT 66666 10000 1 33344)
Q: count total # of employees whose salaries exceed $35000,
but only for dept where more than 1 employee works.
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-6
SQL> select dname,count(*)
2 from dept,employee
3 where dnumber=dno and salary >35000
4 group by dname
5 having count(*) > 1;
no rows selected
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-7
SQL> select dname,count(*)
2 from dept,employee
3 where dnumber=dno and salary >35000 and
4 dno in (select dno
5 from employee
6 group by dno
7 having count(*) > 1)
8 group by dname;
DNAME COUNT(*)
----- ----------
resea 1
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-8
SQL> select dname,count(*),sum(salary)
2 from dept,employee
3 where dnumber=dno and
4 dno in (select dno
5 from employee
6 group by dno
7 having count(*) > 1)
8 group by dname
9 having sum(salary) > 35000;
DNAME COUNT(*) SUM(SALARY)
----- ---------- -----------------
resea 3 100000
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-9
SQL> select * from employee1;
FNAME LNAME SSN SUPERSSN
----- ----- ---------- ----------
johny smith 12345 33344
frank whong 33344 88866
james borgs 88866
SQL> select * from depedent;
FNAME LNAME SSN AGE
----- ----- ---------- ----------
johny smith 12345 4
frank whong 33344 6
johny smith 12345 14
james borgs 88866 16
james borgs 88866 18
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-10
NESTING OF QUERIES
 A complete SELECT query, called a nested query , can be specified
within the WHERE-clause of another query, called the outer query
 Many of the previous queries can be specified in an alternative form
using nesting
 Query : Retrieve the name and address of all employees who work for
the 'Research' department.
Q: SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEE
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research' )
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-11
NESTING OF QUERIES
 The nested query selects the number of the 'Research' department
 The outer query select an EMPLOYEE tuple if its DNO value is in the
result of either nested query
 The comparison operator IN compares a value v with a set (or multi-set)
of values V, and evaluates to TRUE if v is one of the elements in V
 In general, we can have several levels of nested queries
 A reference to an unqualified attribute refers to the relation declared in
the innermost nested query
 In this example, the nested query is not correlated with the outer query
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-12
CORRELATED NESTED
QUERIES
 If a condition in the WHERE-clause of a nested query references an
attribute of a relation declared in the outer query , the two queries are
said to be correlated
 The result of a correlated nested query is different for each tuple (or
combination of tuples) of the relation(s) the outer query
 Query: Retrieve the name of each employee who has a dependent with
the same first name as the employee.
Q: SELECT E.FNAME, E.LNAME
FROM EMPLOYEE AS E
WHERE E.SSN IN (SELECT ESSN
FROM DEPENDENT
WHERE ESSN=E.SSN AND
E.FNAME=DEPENDENT_NAME)
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-13
SQL> select FNAME, LNAME
2 from employee1 E
3 where (select count(*)
4 from depedent
5 where ssn = E.ssn ) >= 2;
where (select count(*)
*
ERROR at line 3:
ORA-00936: missing expression
SQL> select FNAME, LNAME
2 from employee1 E
3 where 2 <= (select count(*)
4 from depedent
5 where ssn = E.ssn );
FNAME LNAME
----- -----
johny smith
james borgs
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-14
ORDER BY
 The ORDER BY clause is used to sort the tuples in a
query result based on the values of some attribute(s)
 Query: Retrieve a list of employees and the projects
each works in, ordered by the employee's department,
and within each department ordered alphabetically by
employee last name.
Q: SELECT DNAME, LNAME, FNAME, PNAME
FROM DEPARTMENT, EMPLOYEE,
WORKS_ON, PROJECT
WHERE DNUMBER=DNO AND SSN=ESSN
AND PNO=PNUMBER
ORDER BY DNAME, LNAME
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-15
ORDER BY (cont.)
 The default order is in ascending order of values
 We can specify the keyword DESC if we want a
descending order; the keyword ASC can be used to
explicitly specify ascending order, even though it is
the default
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-16
Specifying Updates in SQL
There are three SQL commands to modify
the database; INSERT, DELETE, and
UPDATE
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-17
INSERT
In its simplest form, it is used to add one or
more tuples to a relation
Attribute values should be listed in the same
order as the attributes were specified in the
CREATE TABLE command
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-18
INSERT (cont.)
 Example:
U1: INSERT INTO EMPLOYEE
VALUES ('Richard','K','Marini', '653298653', '30-DEC-52',
'98 Oak Forest,Katy,TX', 'M', 37000,'987654321', 4 )
 An alternate form of INSERT specifies explicitly the attribute names
that correspond to the values in the new tuple
 Attributes with NULL values can be left out
 Example: Insert a tuple for a new EMPLOYEE for whom we only
know the FNAME, LNAME, and SSN attributes.
U1A: INSERT INTO EMPLOYEE (FNAME, LNAME, SSN)
VALUES ('Richard', 'Marini', '653298653')
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-19
INSERT (cont.)
 Important Note: Only the constraints specified in
the DDL commands are automatically enforced by
the DBMS when updates are applied to the
database
 Another variation of INSERT allows insertion of
multiple tuples resulting from a query into a
relation
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-20
INSERT (cont.)
– Example: Suppose we want to create a temporary table that has the name,
number of employees, and total salaries for each department. A table
DEPTS_INFO is created by U3A, and is loaded with the summary
information retrieved from the database by the query in U3B.
U3A: CREATE TABLE DEPTS_INFO
(DEPT_NAME VARCHAR(10),
NO_OF_EMPS INTEGER,
TOTAL_SAL INTEGER);
U3B: INSERT INTO DEPTS_INFO (DEPT_NAME,
NO_OF_EMPS, TOTAL_SAL)
SELECT DNAME, COUNT (*), SUM (SALARY)
FROM DEPARTMENT, EMPLOYEE
WHERE DNUMBER=DNO
GROUP BY DNAME ;
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-21
INSERT (cont.)
 Note: The DEPTS_INFO table may not be up-to-date if we
change the tuples in either the DEPARTMENT or the
EMPLOYEE relations after issuing U3B. We have to
create a view (see later) to keep such a table up to date.
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-22
DELETE
 Removes tuples from a relation
 Includes a WHERE-clause to select the tuples to be deleted
 Tuples are deleted from only one table at a time (unless
CASCADE is specified on a referential integrity
constraint)
 A missing WHERE-clause specifies that all tuples in the
relation are to be deleted; the table then becomes an empty
table
 The number of tuples deleted depends on the number of
tuples in the relation that satisfy the WHERE-clause
 Referential integrity should be enforced
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-23
DELETE (cont.)
 Examples:
U4A: DELETE FROM EMPLOYEE
WHERE LNAME='Brown’
U4B: DELETE FROM EMPLOYEE
WHERE SSN='123456789’
U4C: DELETE FROM EMPLOYEE
WHERE DNO IN
(SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
U4D: DELETE FROM EMPLOYEE
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-24
UPDATE
 Used to modify attribute values of one or more
selected tuples
 A WHERE-clause selects the tuples to be modified
 An additional SET-clause specifies the attributes
to be modified and their new values
 Each command modifies tuples in the same
relation
 Referential integrity should be enforced
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-25
UPDATE (cont.)
 Example: Change the location and controlling department
number of project number 10 to 'Bellaire' and 5,
respectively.
U5: UPDATE PROJECT
SET PLOCATION = 'Bellaire', DNUM = 5
WHERE PNUMBER=10
Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition
Copyright © 2004 Ramez Elmasri and Shamkant Navathe
Slide 8-26
UPDATE (cont.)
 Example: Give all employees in the 'Research' department a 10% raise
in salary.
U6: UPDATE EMPLOYEE
SET SALARY = SALARY *1.1
WHERE DNO IN (SELECT DNUMBER
FROM DEPARTMENT
WHERE DNAME='Research')
 In this request, the modified SALARY value depends on the original
SALARY value in each tuple
 The reference to the SALARY attribute on the right of = refers to the old
SALARY value before modification
 The reference to the SALARY attribute on the left of = refers to the new
SALARY value after modification

More Related Content

What's hot

FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAINFP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAINSyahriha Ruslan
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLDHAAROUN
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseSalman Memon
 
Database Keys & Relationship
Database Keys & RelationshipDatabase Keys & Relationship
Database Keys & RelationshipBellal Hossain
 
SQL: Creating and Altering Tables
SQL: Creating and Altering TablesSQL: Creating and Altering Tables
SQL: Creating and Altering TablesRJ Podeschi
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries LectureFelipe Costa
 
İleri Seviye T-SQL Programlama - Chapter 09
İleri Seviye T-SQL Programlama - Chapter 09İleri Seviye T-SQL Programlama - Chapter 09
İleri Seviye T-SQL Programlama - Chapter 09Cihan Özhan
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculusKumar
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQLVikash Sharma
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Proceduresrehaniltifat
 
Oracle sql analytic functions
Oracle sql analytic functionsOracle sql analytic functions
Oracle sql analytic functionsmamamowebby
 
SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functionsAmrit Kaur
 

What's hot (20)

Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAINFP304 DATABASE SYSTEM  PAPER FINAL EXAM AGAIN
FP304 DATABASE SYSTEM PAPER FINAL EXAM AGAIN
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
 
Database Keys & Relationship
Database Keys & RelationshipDatabase Keys & Relationship
Database Keys & Relationship
 
Mysql
MysqlMysql
Mysql
 
SQL: Creating and Altering Tables
SQL: Creating and Altering TablesSQL: Creating and Altering Tables
SQL: Creating and Altering Tables
 
Nested Queries Lecture
Nested Queries LectureNested Queries Lecture
Nested Queries Lecture
 
İleri Seviye T-SQL Programlama - Chapter 09
İleri Seviye T-SQL Programlama - Chapter 09İleri Seviye T-SQL Programlama - Chapter 09
İleri Seviye T-SQL Programlama - Chapter 09
 
5 the relational algebra and calculus
5 the relational algebra and calculus5 the relational algebra and calculus
5 the relational algebra and calculus
 
Sql server T-sql basics ppt-3
Sql server T-sql basics  ppt-3Sql server T-sql basics  ppt-3
Sql server T-sql basics ppt-3
 
PLSQL Cursors
PLSQL CursorsPLSQL Cursors
PLSQL Cursors
 
DATABASE CONSTRAINTS
DATABASE CONSTRAINTSDATABASE CONSTRAINTS
DATABASE CONSTRAINTS
 
Procedures and triggers in SQL
Procedures and triggers in SQLProcedures and triggers in SQL
Procedures and triggers in SQL
 
NESTED SUBQUERY.pptx
NESTED SUBQUERY.pptxNESTED SUBQUERY.pptx
NESTED SUBQUERY.pptx
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
Oracle sql analytic functions
Oracle sql analytic functionsOracle sql analytic functions
Oracle sql analytic functions
 
SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
The Relational Model
The Relational ModelThe Relational Model
The Relational Model
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 

Similar to SQL-examples.ppt (20)

Chapter8
Chapter8Chapter8
Chapter8
 
relAlgebra.ppt
relAlgebra.pptrelAlgebra.ppt
relAlgebra.ppt
 
Unit-3-SQL-part1.ppt
Unit-3-SQL-part1.pptUnit-3-SQL-part1.ppt
Unit-3-SQL-part1.ppt
 
Ch04
Ch04Ch04
Ch04
 
7992267.ppt
7992267.ppt7992267.ppt
7992267.ppt
 
Sql order by clause
Sql order by clauseSql order by clause
Sql order by clause
 
Module 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.pptModule 3 Part I - Bk1 Chapter 07.ppt
Module 3 Part I - Bk1 Chapter 07.ppt
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Week6_Theory.pptx
Week6_Theory.pptxWeek6_Theory.pptx
Week6_Theory.pptx
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Week8_Theory.pptx
Week8_Theory.pptxWeek8_Theory.pptx
Week8_Theory.pptx
 
Lab5 sub query
Lab5   sub queryLab5   sub query
Lab5 sub query
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Class 8 - Database Programming
Class 8 - Database ProgrammingClass 8 - Database Programming
Class 8 - Database Programming
 
Les06 Subqueries
Les06 SubqueriesLes06 Subqueries
Les06 Subqueries
 
Chapter08
Chapter08Chapter08
Chapter08
 
Les12[1]Creating Views
Les12[1]Creating ViewsLes12[1]Creating Views
Les12[1]Creating Views
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Sql select statement
Sql select statementSql select statement
Sql select statement
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 

Recently uploaded

Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdfBreath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdfJess Walker
 
Ahmedabad Escorts Girl Services For Male Tourists 9537192988
Ahmedabad Escorts Girl Services For Male Tourists 9537192988Ahmedabad Escorts Girl Services For Male Tourists 9537192988
Ahmedabad Escorts Girl Services For Male Tourists 9537192988oolala9823
 
Postal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utilisePostal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utiliseccsubcollector
 
办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭o8wvnojp
 
Call Girls In Andheri East Call US Pooja📞 9892124323 Book Hot And
Call Girls In Andheri East Call US Pooja📞 9892124323 Book Hot AndCall Girls In Andheri East Call US Pooja📞 9892124323 Book Hot And
Call Girls In Andheri East Call US Pooja📞 9892124323 Book Hot AndPooja Nehwal
 
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...ur8mqw8e
 
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改atducpo
 
Lilac Illustrated Social Psychology Presentation.pptx
Lilac Illustrated Social Psychology Presentation.pptxLilac Illustrated Social Psychology Presentation.pptx
Lilac Illustrated Social Psychology Presentation.pptxABMWeaklings
 
social media chat application main ppt.pptx
social media chat application main ppt.pptxsocial media chat application main ppt.pptx
social media chat application main ppt.pptxsprasad829829
 
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service DhuleDhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhulesrsj9000
 
REFLECTIONS Newsletter Jan-Jul 2024.pdf.pdf
REFLECTIONS Newsletter Jan-Jul 2024.pdf.pdfREFLECTIONS Newsletter Jan-Jul 2024.pdf.pdf
REFLECTIONS Newsletter Jan-Jul 2024.pdf.pdfssusere8ea60
 
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxJackieSparrow3
 
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ EscortsDelhi Escorts Service
 
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...anilsa9823
 
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road GurgaonCheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road GurgaonDelhi Call girls
 
Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝soniya singh
 
Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666nishakur201
 

Recently uploaded (20)

Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdfBreath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
Breath, Brain & Beyond_A Holistic Approach to Peak Performance.pdf
 
Ahmedabad Escorts Girl Services For Male Tourists 9537192988
Ahmedabad Escorts Girl Services For Male Tourists 9537192988Ahmedabad Escorts Girl Services For Male Tourists 9537192988
Ahmedabad Escorts Girl Services For Male Tourists 9537192988
 
Postal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utilisePostal Ballot procedure for employees to utilise
Postal Ballot procedure for employees to utilise
 
办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭办理西悉尼大学毕业证成绩单、制作假文凭
办理西悉尼大学毕业证成绩单、制作假文凭
 
Call Girls In Andheri East Call US Pooja📞 9892124323 Book Hot And
Call Girls In Andheri East Call US Pooja📞 9892124323 Book Hot AndCall Girls In Andheri East Call US Pooja📞 9892124323 Book Hot And
Call Girls In Andheri East Call US Pooja📞 9892124323 Book Hot And
 
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
《塔夫斯大学毕业证成绩单购买》做Tufts文凭毕业证成绩单/伪造美国假文凭假毕业证书图片Q微信741003700《塔夫斯大学毕业证购买》《Tufts毕业文...
 
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
办理国外毕业证学位证《原版美国montana文凭》蒙大拿州立大学毕业证制作成绩单修改
 
Lilac Illustrated Social Psychology Presentation.pptx
Lilac Illustrated Social Psychology Presentation.pptxLilac Illustrated Social Psychology Presentation.pptx
Lilac Illustrated Social Psychology Presentation.pptx
 
social media chat application main ppt.pptx
social media chat application main ppt.pptxsocial media chat application main ppt.pptx
social media chat application main ppt.pptx
 
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service DhuleDhule Call Girls #9907093804 Contact Number Escorts Service Dhule
Dhule Call Girls #9907093804 Contact Number Escorts Service Dhule
 
REFLECTIONS Newsletter Jan-Jul 2024.pdf.pdf
REFLECTIONS Newsletter Jan-Jul 2024.pdf.pdfREFLECTIONS Newsletter Jan-Jul 2024.pdf.pdf
REFLECTIONS Newsletter Jan-Jul 2024.pdf.pdf
 
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Govindpuri Delhi 💯Call Us 🔝8264348440🔝
 
E J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptxE J Waggoner against Kellogg's Pantheism 8.pptx
E J Waggoner against Kellogg's Pantheism 8.pptx
 
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
(No.1)↠Young Call Girls in Sikanderpur (Gurgaon) ꧁❤ 9711911712 ❤꧂ Escorts
 
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
Lucknow 💋 High Class Call Girls Lucknow 10k @ I'm VIP Independent Escorts Gir...
 
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road GurgaonCheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Cheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
 
escort service sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974
escort service  sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974escort service  sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974
escort service sasti (*~Call Girls in Paschim Vihar Metro❤️9953056974
 
Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝
Call Girls in Kalyan Vihar Delhi 💯 Call Us 🔝8264348440🔝
 
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Lado Sarai Delhi reach out to us at 🔝9953056974🔝
 
Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666Call Girls Anjuna beach Mariott Resort ₰8588052666
Call Girls Anjuna beach Mariott Resort ₰8588052666
 

SQL-examples.ppt

  • 1. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-1 Summary of SQL Queries  A query in SQL can consist of up to six clauses, but only the first two, SELECT and FROM, are mandatory. The clauses are specified in the following order: SELECT <attribute list> FROM <table list> [WHERE <condition>] [GROUP BY <grouping attribute(s)>] [HAVING <group condition>] [ORDER BY <attribute list>]
  • 2. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-2 Summary of SQL Queries (cont.)  The SELECT-clause lists the attributes or functions to be retrieved  The FROM-clause specifies all relations (or aliases) needed in the query but not those needed in nested queries  The WHERE-clause specifies the conditions for selection and join of tuples from the relations specified in the FROM-clause  GROUP BY specifies grouping attributes  HAVING specifies a condition for selection of groups  ORDER BY specifies an order for displaying the result of a query  A query is evaluated by first applying the WHERE-clause, then GROUP BY and HAVING, and finally the SELECT-clause
  • 3. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-3 THE HAVING-CLAUSE  Query: For each project on which more than two employees work , retrieve the project number, project name, and the number of employees who work on that project. SELECT PNUMBER, PNAME, COUNT (*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME HAVING COUNT (*) > 2
  • 4. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-4
  • 5. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-5 SQL> select * from employee; NAME SSN SALARY DNO SUPERSSN ----- ---------- ---------- ---------- ---------- johny 12345 40000 5 33344 frank 33344 30000 5 88866 james 88866 40000 4 77777 borgs 77777 30000 5 88866 (Zhang 55555 35000 4 12345 TTTT 66666 10000 1 33344) Q: count total # of employees whose salaries exceed $35000, but only for dept where more than 1 employee works.
  • 6. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-6 SQL> select dname,count(*) 2 from dept,employee 3 where dnumber=dno and salary >35000 4 group by dname 5 having count(*) > 1; no rows selected
  • 7. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-7 SQL> select dname,count(*) 2 from dept,employee 3 where dnumber=dno and salary >35000 and 4 dno in (select dno 5 from employee 6 group by dno 7 having count(*) > 1) 8 group by dname; DNAME COUNT(*) ----- ---------- resea 1
  • 8. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-8 SQL> select dname,count(*),sum(salary) 2 from dept,employee 3 where dnumber=dno and 4 dno in (select dno 5 from employee 6 group by dno 7 having count(*) > 1) 8 group by dname 9 having sum(salary) > 35000; DNAME COUNT(*) SUM(SALARY) ----- ---------- ----------------- resea 3 100000
  • 9. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-9 SQL> select * from employee1; FNAME LNAME SSN SUPERSSN ----- ----- ---------- ---------- johny smith 12345 33344 frank whong 33344 88866 james borgs 88866 SQL> select * from depedent; FNAME LNAME SSN AGE ----- ----- ---------- ---------- johny smith 12345 4 frank whong 33344 6 johny smith 12345 14 james borgs 88866 16 james borgs 88866 18
  • 10. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-10 NESTING OF QUERIES  A complete SELECT query, called a nested query , can be specified within the WHERE-clause of another query, called the outer query  Many of the previous queries can be specified in an alternative form using nesting  Query : Retrieve the name and address of all employees who work for the 'Research' department. Q: SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research' )
  • 11. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-11 NESTING OF QUERIES  The nested query selects the number of the 'Research' department  The outer query select an EMPLOYEE tuple if its DNO value is in the result of either nested query  The comparison operator IN compares a value v with a set (or multi-set) of values V, and evaluates to TRUE if v is one of the elements in V  In general, we can have several levels of nested queries  A reference to an unqualified attribute refers to the relation declared in the innermost nested query  In this example, the nested query is not correlated with the outer query
  • 12. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-12 CORRELATED NESTED QUERIES  If a condition in the WHERE-clause of a nested query references an attribute of a relation declared in the outer query , the two queries are said to be correlated  The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation(s) the outer query  Query: Retrieve the name of each employee who has a dependent with the same first name as the employee. Q: SELECT E.FNAME, E.LNAME FROM EMPLOYEE AS E WHERE E.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME)
  • 13. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-13 SQL> select FNAME, LNAME 2 from employee1 E 3 where (select count(*) 4 from depedent 5 where ssn = E.ssn ) >= 2; where (select count(*) * ERROR at line 3: ORA-00936: missing expression SQL> select FNAME, LNAME 2 from employee1 E 3 where 2 <= (select count(*) 4 from depedent 5 where ssn = E.ssn ); FNAME LNAME ----- ----- johny smith james borgs
  • 14. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-14 ORDER BY  The ORDER BY clause is used to sort the tuples in a query result based on the values of some attribute(s)  Query: Retrieve a list of employees and the projects each works in, ordered by the employee's department, and within each department ordered alphabetically by employee last name. Q: SELECT DNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDER BY DNAME, LNAME
  • 15. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-15 ORDER BY (cont.)  The default order is in ascending order of values  We can specify the keyword DESC if we want a descending order; the keyword ASC can be used to explicitly specify ascending order, even though it is the default
  • 16. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-16 Specifying Updates in SQL There are three SQL commands to modify the database; INSERT, DELETE, and UPDATE
  • 17. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-17 INSERT In its simplest form, it is used to add one or more tuples to a relation Attribute values should be listed in the same order as the attributes were specified in the CREATE TABLE command
  • 18. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-18 INSERT (cont.)  Example: U1: INSERT INTO EMPLOYEE VALUES ('Richard','K','Marini', '653298653', '30-DEC-52', '98 Oak Forest,Katy,TX', 'M', 37000,'987654321', 4 )  An alternate form of INSERT specifies explicitly the attribute names that correspond to the values in the new tuple  Attributes with NULL values can be left out  Example: Insert a tuple for a new EMPLOYEE for whom we only know the FNAME, LNAME, and SSN attributes. U1A: INSERT INTO EMPLOYEE (FNAME, LNAME, SSN) VALUES ('Richard', 'Marini', '653298653')
  • 19. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-19 INSERT (cont.)  Important Note: Only the constraints specified in the DDL commands are automatically enforced by the DBMS when updates are applied to the database  Another variation of INSERT allows insertion of multiple tuples resulting from a query into a relation
  • 20. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-20 INSERT (cont.) – Example: Suppose we want to create a temporary table that has the name, number of employees, and total salaries for each department. A table DEPTS_INFO is created by U3A, and is loaded with the summary information retrieved from the database by the query in U3B. U3A: CREATE TABLE DEPTS_INFO (DEPT_NAME VARCHAR(10), NO_OF_EMPS INTEGER, TOTAL_SAL INTEGER); U3B: INSERT INTO DEPTS_INFO (DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) SELECT DNAME, COUNT (*), SUM (SALARY) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO GROUP BY DNAME ;
  • 21. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-21 INSERT (cont.)  Note: The DEPTS_INFO table may not be up-to-date if we change the tuples in either the DEPARTMENT or the EMPLOYEE relations after issuing U3B. We have to create a view (see later) to keep such a table up to date.
  • 22. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-22 DELETE  Removes tuples from a relation  Includes a WHERE-clause to select the tuples to be deleted  Tuples are deleted from only one table at a time (unless CASCADE is specified on a referential integrity constraint)  A missing WHERE-clause specifies that all tuples in the relation are to be deleted; the table then becomes an empty table  The number of tuples deleted depends on the number of tuples in the relation that satisfy the WHERE-clause  Referential integrity should be enforced
  • 23. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-23 DELETE (cont.)  Examples: U4A: DELETE FROM EMPLOYEE WHERE LNAME='Brown’ U4B: DELETE FROM EMPLOYEE WHERE SSN='123456789’ U4C: DELETE FROM EMPLOYEE WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research') U4D: DELETE FROM EMPLOYEE
  • 24. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-24 UPDATE  Used to modify attribute values of one or more selected tuples  A WHERE-clause selects the tuples to be modified  An additional SET-clause specifies the attributes to be modified and their new values  Each command modifies tuples in the same relation  Referential integrity should be enforced
  • 25. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-25 UPDATE (cont.)  Example: Change the location and controlling department number of project number 10 to 'Bellaire' and 5, respectively. U5: UPDATE PROJECT SET PLOCATION = 'Bellaire', DNUM = 5 WHERE PNUMBER=10
  • 26. Elmasri and Navathe, Fundamentals of Database Systems, Fourth Edition Copyright © 2004 Ramez Elmasri and Shamkant Navathe Slide 8-26 UPDATE (cont.)  Example: Give all employees in the 'Research' department a 10% raise in salary. U6: UPDATE EMPLOYEE SET SALARY = SALARY *1.1 WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME='Research')  In this request, the modified SALARY value depends on the original SALARY value in each tuple  The reference to the SALARY attribute on the right of = refers to the old SALARY value before modification  The reference to the SALARY attribute on the left of = refers to the new SALARY value after modification

Editor's Notes

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26