100% found this document useful (2 votes)
98 views24 pages

Selecting Data: Duration: 8 Hrs

The document discusses selecting data from databases using SQL queries. It covers selecting columns and rows from single tables using conditions like filters and joins. It also covers combining data from multiple tables using joins, outer joins and subqueries.

Uploaded by

prasadwicum
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
98 views24 pages

Selecting Data: Duration: 8 Hrs

The document discusses selecting data from databases using SQL queries. It covers selecting columns and rows from single tables using conditions like filters and joins. It also covers combining data from multiple tables using joins, outer joins and subqueries.

Uploaded by

prasadwicum
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Selecting Data

Duration : 8 hrs

© 2007, UCSC
Detailed Syllabus
4.3 Selecting Data
4.3.1 Queries: SELECT Statement.

4.3.2 Single Table: all columns (*), selecting specific columns (RA project
operation), unique values (DISTINCT), Executing multiple statements (;),
WHERE clause (RA select operation), Including or excluding rows (=, !=),
Relational Operators (=, !=, >, >=, <, <=), Identifying Null values (IS NULL),
Where clause keywords (AND, OR, [NOT] BETWEEN, [NOT] IN, IS [NOT]
NULL, [NOT] LIKE, ORDER BY, Arithmetic Operators (+, -, *, /),
Expressions, Display Labels, Aggregate Functions: COUNT, SUM, AVG,
MAX, MIN, GROUP BY, HAVING.

4.3.3 Multiple Table: RA join and product operations, Natural Join, Multiple
Table Joins, Aliases for table names, Outer Join, UNION.

4.3.4 Functions: Arithmetic (ROUND, TRUNC), String (TO_CHAR, UPPER,


LOWER, Sub strings, Concatenation, TRIM), Date and Time (DAY,
MONTH, YEAR, DATE, CURRENT).

4.3.5 Sub queries: Nested Select Statement, Values returned by sub queries
(single value, a list of values), EXISTS, Correlated nested queries.

© 2007, UCSC
Referential Integrity
SQL data definition for defining referential integrity constraints
Parent table:
CREATE TABLE DEPARTMENT
(DEPT-NO CHAR(3),
other column definitions
PRIMARY KEY (DEPT-NO) );
Dependent table:
CREATE TABLE EMPLOYEE
( EMP-NO CHAR(5),
DEPT-NO CHAR(3)
other column definitions
PRIMARY KEY (EMP-NO),
FOREIGN KEY DEPT-N-FK (DEPT-NO)
REFERENCES DEPARTMENT
ON DELETE SET NULL) );
© 2007, UCSC
Defining referential integrity rules in the SQL DDL
is known as declarative referential integrity

Declarative referential integrity simplifies application


programming and enables enforcement at the database
server level, eliminating the possibility of programming
errors

User Defined Integrity


User defined integrity constraints can be enforced by the database
server using triggers and stored procedures.

© 2007, UCSC
Triggers and stored procedures are user written
routines which are stored and executed under the
control of the database server.
They are often coded in proprietary procedural
extensions to SQL, e.g. Sybase's Transact SQL or
Oracle's PL/SQL.

SQL for Data Manipulation


Manipulation
SQL allows a user or an application program to update
the database by adding new data, removing old data, and
modifying previously stored data.
© 2007, UCSC
Retrieval
SQL allows a user or an application program
to retrieve stored data from the database
and use it.

• Most Commonly Used Commands


– SELECT INSERT
– UPDATE DELETE

© 2007, UCSC
SQL for Data Manipulation
-High-level Language for data manipulation
-It does not require predefined navigation path

-It does not require knowledge of any key items

-It is uniform language for end-users and


programmers

-It operates on one or more tables based on set


theory, not on a record at a time.

© 2007, UCSC
Command: SELECT
Function
– Retrieves data from one or more rows. Every
SELECT statement produces a table of query
results containing one or more columns and zero
or more rows.
SELECT {[ALL, DISTINCT]}
[(select-item,), i]

• FROM (table specification,)


• {WHERE (search condition)}
• {GROUP BY (group-column,)}
• {HAVING (search condition)}
• {ORDER BY (sort specification,)}
© 2007, UCSC
Project Selected Columns Employee Names
E-No E-Name
179 Silva
857 Perera
342 Dias
Employee SELECT E-No, E-Name
FROM Employee ;
E-No E-Name D-No
179 Silva 7
857 Perera 4
342 Dias 7
Employee Names
E-No E-Name
SELECT E-No, E-Name 342 Dias
857 Perera
FROM Employee
ORDER BY E-Name ; 179 Silva

© 2007, UCSC
Restrict Rows Sales Employee
E-No E-Name D-No
179 Silva 7
342 Dias 7

SELECT *
Employee FROM Employee
E-No E-Name D-No WHERE D-No = '7' ;
179 Silva 7
857 Perera 4
342 Dias 7
Sales Employee
E-No E-Name
SELECT E-No, E-Name
179 Silva
FROM Employee 342 Dias
WHERE D-No = '7' ;

Restrict Rows and Project Columns


© 2007, UCSC
EquiJoin
Employee
Department
E-No E-Name D-No
D-No D-Name M-No
179 Silva 7
857 Perera 4 4 Finance 857
342 Dias 7 7 Sales 179

Emp-Info
E-No E-Name D-No D-No D-Name M-No
179 Silva 7 7 Sales 179
857 Perera 4 4 Finance 857
342 Dias 7 7 Sales 179

SELECT Employee.*, Department.*


FROM Employee, Department
WHERE Employee.D-No = Department.D-No ;
SELECT E.*, D.*
FROM Employee E, Department D
WHERE E.D-No = D.D-No ;
© 2007, UCSC
Inner Join

SELECT E.*, D.*


FROM Employee E
INNER JOIN Department D ON E.D-No = D.D-No;

Outer Joins: Left, Right, Full

SELECT E.*, D.*


FROM Employee E LEFT OUTER JOIN Department D ON E.D-No = D.D-No;

© 2007, UCSC
Department
D-No D-Name M-No
Employee 2 Finance 850
E-No E-Name D-No 7 Sales 179
179 Silva 7
857 Perera 4
342 Dias 7

Emp-Info
E-No E-Name D-No D-No D-Name M-No
179 Silva 7 7 Sales 179
857 Perera 4 Null Null Null
342 Dias 7 7 Sales 179
© 2007, UCSC
Emp-Info
E-No E-Name D-No D-No D-Name M-No
Null Null Null 2 Finance 850

179 Silva 7 7 Sales 179

342 Dias 7 7 Sales 179

Right Outer Join


SELECT E.*, D.*
FROM Employee E RIGHT OUTER JOIN Department D
ON E.D-No = D.D-No;
Full Outer Join
SELECT E.*, D.*
FROM Employee E FULL OUTER JOIN Department D
ON E.D-No = D.D-No;

© 2007, UCSC
Cartesian Product
Department
D-No D-Name M-No
4 Finance 857
7 Sales 179
Employee
E-No E-Name D-No
179 Silva 7
857 Perera 4
342 Dias 7

Emp-Info
SELECT
E-No E-Name D-No D-No D-Name M-No
E.*, D.*
FROM 179 Silva 7 4 Finance 857
857 Perera 4 4 Finance 857
Employee E, Department D
342 Dias 7 4 Finance 857
179 Silva 7 7 Sales 179
857 Perera 4 7 Sales 179
342 Dias 7 7 Sales 179

© 2007, UCSC
SQL Data Retrieval
Basic Search Conditions
Comparison
– Equal to =
– Not equal to != or <> or ^=
– Less than to <
– Less than or equal to <=
– Greater than to >
– Greater than or equal to >=

© 2007, UCSC
SQL Data Retrieval
Basic Search Conditions (cont’d)
• Range ( [NOT] BETWEEN)

– expres-1 [NOT] BETWEEN expres-2 AND expres- 3

– Example: WEIGHT BETWEEN 50 AND 60

• Set Membership ( [NOT] IN)

– Example 1: WHERE Emp_No IN (‘E1’, ‘E2’, ‘E3’)

– Example 2: WHERE Emp_No IN (Select Emp_No


FROM Employee WHERE Dept_No=‘7’)

© 2007, UCSC
Basic Search Conditions (cont’d) :
• Pattern Matching ([NOT] LIKE)
– expres-1 [NOT] LIKE {special-register | host-
variable | string-constant}
– Example: WHERE Proj_Name LIKE
“INFORM%”

• Null Value (IS [NOT] NULL)


– Example: WHERE Proj_Name IS NOT
NULL

© 2007, UCSC
Compound Search Conditions
AND, OR and NOT
Example:
WHERE Proj_Name LIKE ‘INFORM%’ AND Emp_Name = ‘DIAS’

SQL Query Features


• Summary Queries
– Summarize data from the database. In general, summary queries use
SQL functions to collapse a column of data values into a single value
that summarizes the column. (AVG, MIN, MAX, SUM, COUNT..)

• Sub-Queries
– Use the results of one query to help define another query

© 2007, UCSC
Summarising Data
SELECT COUNT(*)
FROM Employee

Count(*)
Employee 5
E-No Job Salary D-No
179 Manager 20000 10
857 Clerk 8000 10
342 Clerk 9000 20
477 Manager 15000 30
432 Clerk 10000 30
SELECT AVG(Salary) AVG(Salary)
FROM Employee 12400
© 2007, UCSC
SELECT STATEMENT
May also contain
[GROUP BY [HAVING] ORDER BY]
GROUP BY
A result of a previous specified clause is grouped using the group
by clause.
e.g. SELECT d-no, AVG(salary)
FROM employee
GROUP BY d-no

Employee
E-No Job Salary D-No
179 Manager 20000 10 D-No AVG(Salary)
857 Clerk 8000 10 10 14,000
342 Clerk 9000 20 20 9,000
477 Manager 15000 30 30 12,500
432 Clerk 10000 30 © 2007, UCSC
[GROUP BY [HAVING] ORDER BY]

HAVING
Used for select groups that meet
specified conditions.

Always used with GROUP BY clause.


SELECT d-no, AVG(salary)
FORM employee
GROUP BY d-no
HAVING AVG(salary)>12000
Employee
E-No Job Salary D-No
179 Manager 20000 10
857 Clerk 8000 10 D-No AVG(Salary)
342 Clerk 9000 20 10 14,000
477 Manager 15000 30 30 12,500
432 Clerk 10000 30 © 2007, UCSC
Nested Queries
A sub query is SELECT statement that nest inside the WHERE
clause of another SELECT statement. The results are need in
solving the main query.
Get a list of all suppliers supplying part P2.
SELECT sname
FROM supplier
WHERE sno IN
(SELECT sno FROM supply WHERE pno = ‘P2’);

SELECT sname
FROM supplier, supply
WHERE supplier.sno = supply.sno and pno = ‘P2’;

SELECT ename , salary


FROM employee
WHERE salary = (SELECT MIN (salary) FROM employee)
© 2007, UCSC
Nested Queries Contd.
Sub queries with EXISTS
e.g. find all publishers who
publish business books

SELECT DISTINCT pub_name


FROM publishers
WHERE EXISTS
(SELECT * FROM title
WHERE pub_id = publishers.pub_id and type = “business”)

DISTINCT – will remove multiple occurrences

© 2007, UCSC

You might also like