0% found this document useful (0 votes)
69 views25 pages

11 - SELECT Statement PDF

The document discusses the SELECT statement in SQL. It describes the functionality and syntax of the SELECT statement. The SELECT statement retrieves data from a relational database and the results can be considered a transient or persistent relational table. Examples are provided to demonstrate how to write SELECT statements to query one or more tables and return selected rows and columns.

Uploaded by

Zalikha
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
69 views25 pages

11 - SELECT Statement PDF

The document discusses the SELECT statement in SQL. It describes the functionality and syntax of the SELECT statement. The SELECT statement retrieves data from a relational database and the results can be considered a transient or persistent relational table. Examples are provided to demonstrate how to write SELECT statements to query one or more tables and return selected rows and columns.

Uploaded by

Zalikha
Copyright
© © All Rights Reserved
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/ 25

30/09/2021, 14:16 SELECT statement (1)

 CSIT115 Data Management and Security

SELECT statement (1)


School of Computing and Information Technology -
University of Wollongong

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 1/25


30/09/2021, 14:16 SELECT statement (1)

SELECT statement (1)


Outline

Functionality and syntax


Projection queries
Queries with row functions
Queries with group functions
Special queries

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 2/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 2/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


Functionality
- SELECT statement retrieves data from a relational database
- The results of SELECT statement can be considered as a transient relational
table
- The results of SELECT statement can be saved as a persistent relational table

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 3/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 3/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


Selection queries select complete rows from a relational table

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 4/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 4/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


Projection queries select complete columns from a relational table

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 5/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 5/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


Sample database
CREATE TABLE DEPARTMENT(
CREATE TABLE statement
name VARCHAR(50) NOT NULL,

code CHAR(5) NOT NULL,

total_staff_number DECIMAL(2) NOT NULL,

chair VARCHAR(50) NULL,

budget DECIMAL(9,1) NULL, /* In this presentation */

CONSTRAINT dept_pkey PRIMARY KEY(name), /* budget can be NULL */

CONSTRAINT dept_ckey1 UNIQUE(code),


CONSTRAINT dept_ckey2 UNIQUE(chair),

CONSTRAINT dept_check1 CHECK (total_staff_number BETWEEN 1 AND 50) );

CREATE TABLE COURSE(


CREATE TABLE statement
cnum CHAR(7) NOT NULL,

title VARCHAR(200) NOT NULL,

credits DECIMAL(2) NOT NULL,

offered_by VARCHAR(50) NULL,

CONSTRAINT course_pkey PRIMARY KEY(cnum),

CONSTRAINT course_check1 CHECK (credits IN (6, 12)),

CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)

REFERENCES DEPARTMENT(name) ON DELETE CASCADE );


TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 6/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 6/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


Examples
- Find a name and code of a department that has more than 30 staff members
SELECT code, name
SELECT statement
FROM DEPARTMENT

WHERE total_staff_number > 30;

- 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

WHERE offered_by IN ( SELECT name


FROM DEPARTMENT

WHERE chair = 'James Bond' );


TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 7/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 7/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


Examples
- Find all information about courses offered by a department whose chair is
James Bond
SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name

FROM DEPARTMENT

WHERE chair = 'James Bond' ) JB

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

WHERE chair = 'James Bond' ),

JAMESCOURSE AS

( SELECT *

FROM COURSE JOIN JAMES

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 8/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 8/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


    Keywords
SELECT code, name
SELECT statement
FROM DEPARTMENT

WHERE total_staff_number > 30;

SELECT code, cnum, title


SELECT statement
FROM DEPARTMENT, COURSE

WHERE name = offered_by AND credits = 6;

SELECT *
Nested SELECT statement
FROM COURSE

WHERE offered_by IN ( SELECT name


FROM DEPARTMENT

WHERE chair = 'James Bond' );

SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name

FROM DEPARTMENT

WHERE chair = 'James Bond' ) JB

ON COURSE.offered_by = JB.name;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 9/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 9/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


              Selected columns
SELECT code, name
SELECT statement
FROM DEPARTMENT

WHERE total_staff_number > 30;

SELECT code, cnum, title


SELECT statement
FROM DEPARTMENT, COURSE

WHERE name = offered_by AND credits = 6;

SELECT *
Nested SELECT statement
FROM COURSE

WHERE offered_by IN ( SELECT name


FROM DEPARTMENT

WHERE chair = 'James Bond' );

SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name

FROM DEPARTMENT

WHERE chair = 'James Bond' ) JB

ON COURSE.offered_by = JB.name;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 10/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 10/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


            Relational tables used
SELECT code, name
SELECT statement
FROM DEPARTMENT

WHERE total_staff_number > 30;

SELECT code, cnum, title


SELECT statement
FROM DEPARTMENT, COURSE

WHERE name = offered_by AND credits = 6;

SELECT *
Nested SELECT statement
FROM COURSE

WHERE offered_by IN ( SELECT name


FROM DEPARTMENT

WHERE chair = 'James Bond' );

SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name

FROM DEPARTMENT

WHERE chair = 'James Bond' ) JB

ON COURSE.offered_by = JB.name;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 11/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 11/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


             Conditions
SELECT code, name
SELECT statement
FROM DEPARTMENT

WHERE total_staff_number > 30;

SELECT code, cnum, title


SELECT statement
FROM DEPARTMENT, COURSE

WHERE name = offered_by AND credits = 6;

SELECT *
Nested SELECT statement
FROM COURSE

WHERE offered_by IN ( SELECT name


FROM DEPARTMENT

WHERE chair = 'James Bond' );

SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name

FROM DEPARTMENT

WHERE chair = 'James Bond' ) JB

ON COURSE.offered_by = JB.name;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 12/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 12/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


                                    Subqueries
SELECT *
Nested SELECT statement
FROM COURSE

WHERE offered_by IN ( SELECT name


FROM DEPARTMENT

WHERE chair = 'James Bond' );

SELECT COURSE.*
SELECT statement with inline view
FROM COURSE JOIN ( SELECT name

FROM DEPARTMENT

WHERE chair = 'James Bond' ) JB

ON COURSE.offered_by = JB.name;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 13/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 13/25


30/09/2021, 14:16 SELECT statement (1)

Functionality and syntax


WITH clause
WITH JAMES AS
Nested SELECT statement
( SELECT name

FROM DEPARTMENT

WHERE chair = 'James Bond' ),

JAMESCOURSE AS

( SELECT *

FROM COURSE JOIN JAMES

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

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 14/25


30/09/2021, 14:16 SELECT statement (1)

SELECT statement (1)


Outline

Functionality and syntax


Projection queries
Queries with row functions
Queries with group functions
Special queries

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 15/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 15/25


30/09/2021, 14:16 SELECT statement (1)

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;

Find the names and chairpersons of all departments


SELECT name, chair
sql
FROM DEPARTMENT;

Find the titles of all courses


SELECT title
sql
FROM COURSE;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 16/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 16/25


30/09/2021, 14:16 SELECT statement (1)

Projection queries with duplicates/ no duplicates


A keyword DISTINCT can be used to eliminated duplicates from the
results of a query
Find the credit points of all courses
SELECT credits
sql
FROM COURSE;

Find the distinct credit points of all courses


SELECT DISTINCT credits
sql
FROM COURSE;

Find the distinct total number of staff members in each


department
SELECT DISTINCT total_staff_number
sql
FROM DEPARTMENT;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 17/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 17/25


30/09/2021, 14:16 SELECT statement (1)

SELECT statement (1)


Outline

Functionality and syntax


Projection queries
Queries with row functions
Queries with group functions
Special queries

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 18/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 18/25


30/09/2021, 14:16 SELECT statement (1)

Queries with row functions


A row function is called and it is processed one time for each row
selected from a relational table
List the names of departments in uppercase format
SELECT UPPER(name)
UPPER row function
FROM DEPARTMENT;

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;

Display the name of departments and budgets increased by 10%


SELECT name, 1.1*IFNULL(budget,0)
IFNULL row function
FROM DEPARTMENT;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 19/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 19/25


30/09/2021, 14:16 SELECT statement (1)

SELECT statement (1)


Outline

Functionality and syntax


Projection queries
Queries with row functions
Queries with group functions
Special queries

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 20/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 20/25


30/09/2021, 14:16 SELECT statement (1)

Queries with group functions


A group function is called and it is processed one time for a group of
rows
Find the total number of courses
SELECT COUNT(*)
COUNT group function
FROM COURSE;

Find the total number of all staff members in all departments


SELECT SUM(total_staff_number)
SUM group function
FROM DEPARTMENT;

Find an average budget per each department


SELECT AVG(IFNULL(budget, 0))
AVG group function
FROM DEPARTMENT;

Find the total number of staff members in the largest department


SELECT MAX(total_staff_number)
MAX group function
FROM DEPARTMENT;
TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 21/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 21/25


30/09/2021, 14:16 SELECT statement (1)

SELECT statement (1)


Outline

Functionality and syntax


Projection queries
Queries with row functions
Queries with group functions
Special queries

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 22/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 22/25


30/09/2021, 14:16 SELECT statement (1)

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;

Add 2 months to a current date


SELECT DATE_ADD(SYSDATE(), INTERVAL 2 MONTH )
Date arithmetic
FROM DUAL;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 23/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 23/25


30/09/2021, 14:16 SELECT statement (1)

Special queries
How many days have passed since 1 January 2001 ?
SELECT DATEDIFF(SYSDATE(),'2001-01-01')
Date arithmetic
FROM DUAL;

SQL as word processor


Who am I ?
SELECT CONCAT('I am ', CURRENT_USER())
String concatenation and user name
FROM DUAL;

Hello world !
SELECT 'Hello world !'
The famous Hello world ! program
FROM DUAL;

Substring of 'Hello world' that starts from the first 'e'



String processing
SELECT SUBSTR('Hello world', INSTR('Hello world','e'), LENGTH('Hello world'))

FROM DUAL;

TOP
             
Created by Janusz R. Getta,  CSIT115/CSIT815 Data Management and Security,  Summer 2020/2021 24/25

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 24/25


30/09/2021, 14:16 SELECT statement (1)

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

file:///C:/Users/garyng/Desktop/CSIT 115 Data Management Security/Oct 2021/11select-1/11select-1/11select-1.html#1 25/25

You might also like