11 - SELECT Statement PDF
11 - SELECT Statement PDF
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 2/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 3/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 4/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 5/25
- Find a code of a department and course number such that a course is offered
by a department and a course provides 6 credit points
SELECT DEPARTMENT.code, COURSE.cnum, title
SELECT statement with JOIN operation
FROM DEPARTMENT JOIN COURSE
ON DEPARTMENT.name = COURSE.offered_by
WHERE credits = 6;
- Find all information about courses offered by a department whose chair is
James Bond
SELECT *
Nested SELECT statement
FROM COURSE
FROM DEPARTMENT
ON COURSE.offered_by = JB.name;
- Find all information about courses offered by a department whose chair is
James Bond
WITH JAMES AS
SELECT statement with WITH clause
( SELECT name
FROM DEPARTMENT
JAMESCOURSE AS
( SELECT *
ON COURSE.offered_by = JAMES.name )
SELECT *
Nested SELECT statement
FROM COURSE
SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name
FROM DEPARTMENT
ON COURSE.offered_by = JB.name;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 9/25
SELECT *
Nested SELECT statement
FROM COURSE
SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name
FROM DEPARTMENT
ON COURSE.offered_by = JB.name;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 10/25
SELECT *
Nested SELECT statement
FROM COURSE
SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name
FROM DEPARTMENT
ON COURSE.offered_by = JB.name;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 11/25
SELECT *
Nested SELECT statement
FROM COURSE
SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name
FROM DEPARTMENT
ON COURSE.offered_by = JB.name;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 12/25
SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name
FROM DEPARTMENT
ON COURSE.offered_by = JB.name;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 13/25
FROM DEPARTMENT
JAMESCOURSE AS
( SELECT *
ON COURSE.offered_by = JAMES.name )
SELECT JAMESCOURSE.*
FROM JAMESCOURSE;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 14/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 15/25
Projection queries
Projection queries select the entire columns from a relational table and
do not have WHERE clause
Find full information about all departments
SELECT code, name, total_staff_number, chair, budget
sql
FROM DEPARTMENT;
SELECT *
sql
FROM DEPARTMENT;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 16/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 17/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 18/25
Find the first three characters from all course codes and
full titles of all
courses
SELECT SUBSTR(cnum, 1, 3), title
SUBSTR row function
FROM COURSE;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 19/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 20/25
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 22/25
Special queries
SQL as a calculator
Compute 30 hours * $90.30 per hour
SELECT 30 * 90.30
Arithmetic expression in SELECT clause
FROM DUAL;
SQL as a diary
What date is tomorrow ?
SELECT DATE_ADD(SYSDATE(), INTERVAL 1 DAY)
Date arithmetic
FROM DUAL;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 23/25
Special queries
How many days have passed since 1 January 2001 ?
SELECT DATEDIFF(SYSDATE(),'2001-01-01')
Date arithmetic
FROM DUAL;
Hello world !
SELECT 'Hello world !'
The famous Hello world ! program
FROM DUAL;
FROM DUAL;
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 24/25
References
C. Coronel, S. Morris, A. Basta, M. Zgola, Data Management and Security,
Chapter 5 Introduction to Structured Query Language, Cengage
Compose eBook, 2018, eBook: Data Management and Security, 1st
Edition
T. Connoly, C. Begg, Database Systems, A Practical Approach to Design,
Implementation, and Management, Chapters 6.3.1 - 6.3.4 Data
Manipulation, Pearson Education Ltd, 2015
D. Darmawikarta, SQL for MySQL A Beginner’s Tutorial, Chapters 2 - 5,
Brainy Software Inc. First Edition: June 2014
How to ... ? Cookbook, How to implement queries in SQL, Recipe 5.1 How
to implement SELECT statements with simple Boolean expressions ?
TOP
Created by Janusz R. Getta, CSIT115/CSIT815 Data Management and Security, Summer 2020/2021 25/25