SQL Querying and Data Manipulation: SELECT, Joined Tables, Nested Queries, and Updates, Study Guides, Projects, Research of Introduction to Database Management Systems
An overview of sql querying and data manipulation techniques, including select statements, joined tables, nested queries, and updates. Topics covered include conceptual processing of select statements, joined tables in from clauses, nested queries, correlated nested queries, and the exists function. The document also explains how to use explicit sets in the where clause and handle null values in sql queries.
Download SQL Querying and Data Manipulation: SELECT, Joined Tables, Nested Queries, and Updates and more Study Guides, Projects, Research Introduction to Database Management Systems in PDF only on Docsity!
Announcements
•^ Written Homework 1 due Friday– If you have fourth edition make sure you do the rightproblems•^ Program 3 out today, due next Friday Nov 10•^ Today– Finish up Chapter 8•^ Reading– Skim Chapters 9 & 26– Chapter 10
From Last Time
“Get employee info of all employees that earn themaximum salary”
SELECT
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
optionalclauses
SELECT SELECTSTATEMENT
manytables
singletable
input to a SELECT statement is N tablesoutput is a single table
Joined Tables in FROM
•^ General from is FROM
•^ Tables list can include “Joined” tables“Retrieve name and address of all employeeswho work in “research” department”^ SELECT
fname,
lname,
address
FROM^ (employee
JOIN^
department
ON^ dno=dnumber)
WHERE^
dname^ =^ ‘Research’
•^ Condition dno=dnumber could also be in theWHERE clause
Joined Tables II
“For each employee, retrieve his/her last nameand supervisors last name, or NULL if he/shehas no supervisor” SELECT
e.lname
as^ employee_name, s.lname
as^ supervisor_name FROM^
(^ employee
e^ LEFT
OUTER
JOIN^
employee
s
ON^ e.superssn
=^ s.ssn);
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 alternativeform using nesting• Query 1: Retrieve the name and address of allemployees who work for the 'Research' department.^ SELECT
FNAME,
LNAME,
ADDRESS
FROM^
EMPLOYEE
WHERE^
DNO^ IN
( SELECT
DNUMBER
FROM^
DEPARTMENT
WHERE^
DNAME='Research'
NESTING OF QUERIES (contd.)
•^ The nested query selects the number of the 'Research'department•^ The outer query select an EMPLOYEE tuple if its DNOvalue is in the result of either nested query•^ The comparison operator IN compares a value v with aset (or multi-set) of values V, and evaluates to TRUE if vis 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
CORRELATED NESTED QUERIES (contd.)•^ In Q12, the nested query has a different result in theouter query•^ A query written with nested SELECT... FROM...WHERE... blocks and using the = or IN comparisonoperators can
always
be expressed as a single block
query. For example, Q12 may be written as in Q12AQ12A:
SELECT
E.FNAME, E.LNAME
FROM^
EMPLOYEE E, DEPENDENT D
WHERE
E.SSN=D.ESSN ANDE.FNAME=D.DEPENDENT_NAME
THE EXISTS FUNCTION
•^ EXISTS is used to check whether the result of acorrelated nested query is empty (contains notuples) or not– We can formulate Query 12 in an alternative form thatuses EXISTS as Q12B
EXPLICIT SETS
•^ It is also possible to use an
explicit
(enumerated) set of values
in the WHERE-
clause rather than a nested query• Query 13: Retrieve the social security numbersof all employees who work on project number 1,2, or 3.Q13:^
SELECT
DISTINCT ESSN
FROM^
WORKS_ON
WHERE
PNO IN (1, 2, 3)
NULLS IN SQL QUERIES
•^ SQL allows queries that check if a value is
NULL
(missing or undefined or not applicable)• SQL uses
IS^ or^ IS NOT
to compare NULLs because it
considers each NULL value distinct from other NULLvalues, so
equality comparison is not appropriate
•^ Query 14: Retrieve the names of all employees who donot have supervisors.Q14:
SELECT
FNAME, LNAME
FROM^
EMPLOYEE
WHERE
SUPERSSN IS NULL
Note: If a join condition is specified, tuples with NULL values forthe join attributes are not included in the result
INSERT
•^ In its simplest form, it is used to add one or moretuples to a relation•^ Attribute values should be listed in the sameorder as the attributes were specified in the^ CREATE TABLE
command
INSERT (contd.)
•^ 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 theattribute names that correspond to the values in the newtuple– Attributes with NULL values can be left out•^ Example: Insert a tuple for a new EMPLOYEE for whomwe only know the FNAME, LNAME, and SSN attributes.U1A: