UNIT 04-P3 Database Programming Using SQL
UNIT 04-P3 Database Programming Using SQL
3 - 1
UNIT 04 Part-3
Database Programming
– Queries in SQL
Introduction
SQL Basic Query Structure: Queries from
One Table
Queries from Multiple Tables
Join Queries
Set Queries
Projection attributes
Attributes whose values are to be retrieved
Conditions
Selection Condition: Boolean condition that must
be true for any retrieved tuple.
Join Condition: Boolean condition on join
attributes when multiple relations are involved.
Logical comparison operators
=, <, <=, >, >=, and <>
Note:
It is extremely important not to overlook specifying any
selection and join conditions in the WHERE-clause;
otherwise, incorrect and very large relations may result
SELECT *
FROM EMPLOYEE
ORDER BY Lname ASC;
SELECT *
FROM EMPLOYEE
ORDER BY SALARY DESC;
Join Condition
Selection Condition
EMPLOYEE DEPARTMENT
SSN DNO Dnumber Name
Join 5 Research
Join Conditions
Selection Condition
EMPLOYEE DEPARTMENT
SSN DNO Mgr_
Dnumber DName SSN
Join (2)
Join (1)
Join (2)
Results of Project
result
UNION MINUS
INTERSECT
Null
1
2
Example:
SELECT Fname, Lname
FROM EMLOYEE
WHERE (Salary BETWEEN 30000 AND 40000);
Example
Result
Consider the following relations for a database that keeps track of student
enrollment in courses and the books adopted for each course: