Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Kingdom of Saudi Arabia

Ministry of Higher Education


King Faisal University
College of Computer Sciences &
Information Technology

IS222 – DATABASE
CONCEPTS AND DESIGN
Lab sheet
Lab 8: SELECT statement with WHERE

IS222
Table of Contents
LAB 8: SELECT Statement with WHERE..............................................................................2
Objectives:..........................................................................................................................................2
Tools/Software:..................................................................................................................................2
Concepts & Descriptions:.................................................................................................................2
SELCET Statement with WHERE..................................................................................................2
Examples and explanations of SELECT statement with WHERE...............................................3
Examples and explanations of SELECT statement with DISTINCT...........................................4
LIKE Operator..................................................................................................................................5
Examples and explanations of SELECT with LIKE operator......................................................5
Lab Activities:....................................................................................................................................6
Deliverables:.......................................................................................................................................7
LAB 8: SELECT Statement with WHERE
Objectives:
This lesson covers the following objectives:

 Create SELECT statements using comparison operators


 Create SELECT statements using Between, AND, In, and OR.
 Create SQL statements using Like.
 Create SELECT statements using Is Null

Tools/Software:
To accomplish this session, students should have access to APEX.

Concepts & Descriptions:


SELCET Statement with WHERE
 The WHERE is extract only record that specified condition.
 The WHERE with AND requires that two conditions are true.
 The WHERE with OR requires one of two conditions is true.
 The WHERE with NOT is extract only record that negates condition.
 The WHERE with IN is used to multiple values.
 DISTINCT statement is used to avoid duplicate in retrieve data.
Examples and explanations of SELECT statement with WHERE

Syntax of SELECT statement with WHERE

SELECT <column1>, … <columnN> FROM <table_name> WHERE <condition>;

Type of condition SQL command example Explanation


SELECT * FROM students WHERE  This command retrieves all columns from
stud_age < 22; students table that their ages less than 2

SELECT stud_name FROM students  This command retrieves student name


Using Comparison Operators
WHERE stu_BOD > TO_DATE ('1-1- column from students table that their bi
1990','dd-mm-yyyy') days after 1-Jan-1990.

Using where with BETWEEN SELECT stud_name FROM students  This command retrieves student name
WHERE stud_GPA BETWEEN 3.5 AND column from students table that their GP
4.5 are not less than 3.5 and not more than

Using where with IN SELECT stud_name FROM students  This command retrieves student name
WHERE stud_dept IN ('IS','CS') column from students table that their
departments are either IS or CS.

Using where with AND Select *  This command retrieves all columns from
FROM students students table that named Ahmed and th
WHERE stud_name='Ahmed' AND ages are greater than 19.
stud_age >19
Using where with OR SELECT stud_name,stud_id FROM  This command retrieves student name a
students WHERE Stud_name = student_id columns from student table t
'Ahmed' OR stud_age =20 named Ahmed, or their ages is 20.

using where with NOT SELECT * FROM students WHERE NOT  This command retrieves all columns from
Stud_name ='Ahmed' students table that named not Ahmed.

Using where with IS NULL SELECT * FROM students WHERE  This command retrieves all columns from
stud_dept IS NULL students table whose stud_dept is null
(empty).
Examples and explanations of SELECT statement with DISTINCT

Syntax of SELECT statement with DISTINCT

SELECT DISTINCT <column1>, … <columnN> FROM <table_name> WHERE <condition>;

Table 1: students table

stud_id stud_name stud_age


10 Ahmed 20
20 Ali 18
30 Ahmed 24

Type SQL command example Explanations Output


Without DISTINCT SELECT stud_name FROM students This command retrieves all student name in Ahmed
students table. Ali
Ahmed
Using DISTINCT SELECT DISTINCT stud_name FROM  This command retrieves only the unique Ahmed
students student name in student table. Ali
 The name that is repeated multiple time in
the table this command only retrieves it
one time.
LIKE Operator
 LIKE operator is used to find a specific pattern in a column.
 The character '%' represents none, one or more characters.
 The character '_' represents one and only one character.

Examples and explanations of SELECT with LIKE operator


Type of condition SQL command example Explanation
Search based on first SELECT * FROM student WHERE This command retrieves all column from
character stud_name LIKE 'a%' students table that their students name star
with ‘a’ letter.

Search based on last SELECT * FROM student WHERE This command retrieves all column from
character stud_name LIKE '%a' students table that their students name end
‘a’ letter.

Search based on first and SELECT * FROM student WHERE This command retrieves all column from
last character stud_name LIKE 'a%d' students table that their students name star
with ‘a’ letter and end with ‘d’ letter.

Search based on character in SELECT * FROM student WHERE This command retrieves all column from
any position stud_name LIKE '%a%' students table that their students name
contained of ‘a’ letter.

SELECT * FROM students WHERE  This command retrieves all column from
Stud_name LIKE '_n%' students table that their second letter of
students’ name are’ n’.
Search based on second  The 1st character can be anything, the 2
character must be ‘n’ and the next characters can
none, one or more.

SELECT * FROM students WHERE  This command retrieves all column from
Stud_name LIKE '_n_' students table that their second letter of
students’ name are’ n’.
 The 1st character can be anything ,2nd
character 'n' and any 3rd character. The
name should consist of only 3 letters.
Lab Activities:

1. Write the SQL command to retrieve all columns from MOVIES table that have R
rating and their categories are DRAMA.

SELECT * FROM MOVIES WHERE RATING = 'R' AND CATEGORY = 'DRAMA';

2. Write the SQL command to retrieve Title of movies from MOVIES table that their
categories are FANTASY or DRAMA.

SELECT TITLE FROM MOVIES WHERE CATEGORY = 'FANTASY' OR CATEGORY =


'DRAMA';

3. Write the SQL command to retrieve all records from MOVIES table excepted
records that their categories are ANIMATION.
SELECT * FROM MOVIES WHERE CATEGORY <> 'ANIMATION';

4. Write the SQL command to retrieve all columns from MOVIES table that have PG
Rating and their categories are DRAMA OR FANTASY.

SELECT * FROM MOVIES WHERE RATING = 'PG' AND (CATEGORY = 'DRAMA' OR


CATEGORY = 'FANTASY');

5. Write the SQL command to retrieve all columns from MOVIES table that their
titles start with ‘a’ letter.

SELECT * FROM MOVIES WHERE TITLE LIKE 'a%';

6. Write the SQL command to retrieve all movies from MOVIES table that their
titles end with ‘s’ letter.

SELECT * FROM MOVIES WHERE TITLE LIKE '%s';

7. Write the SQL command to retrieve title of movies from MOVIES table that their
titles end with ‘s’ letter and start with ’R’ letter.

SELECT TITLE FROM MOVIES WHERE TITLE LIKE 'R%s';

8. Write the SQL command to retrieve title of movies from MOVIES table that third
letter of their titles is ’e’.
SELECT TITLE FROM MOVIES WHERE TITLE LIKE '__e%';

Deliverables:
At the end of this session, students are expected to:
 Solve the lab activities and submit their work.

You might also like