You are on page 1of 37

SINGLE-TABLE QUERIES

Objectives:
 In this lecture you will learn about the SQL SELECT command that is used to
retrieve data in a database.

 You will also learn ways to sort data and use functions to count rows and
calculate totals.

 You will learn as well about a special feature of SQL that allows you to nest
SELECT commands – placing one SELECT command inside another SELECT
command

 Finally, you will learn how to group rows that have matching values in some
column.
STUDENTS TABLE
ID Num LName FName MName Age Gender
001 Sison Carlo Sy 16 Male
002 Cruz Carla Uy 16 Female
003 Dela Cruz Pedro Ang 17 Male
004 San Jose Juan Tan 17 Male
005 Santos Chad Moreno 17 Male
006 Angeles Joan Reyes 16 Female
007 Bauzon Janice Tan 18 Female
008 Gomez Richard Cruz 22 Male
009 Corpuz Jackie Montefalco 20 Female
010 Bartolome Jade Corpuz 20 Female
RETRIEVING ALL COLUMNS & ROWS IN A TABLE
 SYNTAX:
Asterisk indicates
SELECT * FROM Tablename; that all columns Student is the name
 Using Mysql Console: and rows will be of the table whose
mysql > SELECT * included columns & rows will
-> FROM Students; be displayed

ID Num LName FName MName Age Gender


001 Sison Carlo Sy 16 Male
002 Cruz Carla Uy 16 Female
003 Dela Cruz Pedro Ang 17 Male
004 San Jose Juan Tan 17 Male
005 Santos Chad Moreno 17 Male
006 Angeles Joan Reyes 16 Female
007 Bauzon Janice Tan 18 Female
008 Gomez Richard Cruz 22 Male
009 Corpuz Jackie Montefalco 20 Female
010 Bartolome Jade Corpuz 20 Female
RETRIEVING CERTAIN COLUMNS & ALL ROWS IN A TABLE
 SYNTAX:
SELECT Field_1, Field_2, ... Field_x FROM Tablename; Fields indicates the
 Using Mysql Console: specific columns to
mysql > SELECT ID Num, Lname, Fname, MName be displayed.
-> FROM Students;
ID Num LName FName MName
001 Sison Carlo Sy
002 Cruz Carla Uy
Student is the name 003 Dela Cruz Pedro Ang
of the table whose
specific columns & 004 San Jose Juan Tan
all rows will be 005 Santos Chad Moreno
displayed 006 Angeles Joan Reyes
007 Bauzon Janice Tan
008 Gomez Richard Cruz
009 Corpuz Jackie Montefalco
010 Bartolome Jade Corpuz
USING WHERE CLAUSE
 SYNTAX:
SELECT Field_1, Field_2, ... Field_x
FROM Tablename
WHERE Condition;

 Using Mysql Console:


mysql > SELECT ID Num, Lname, Fname, MName
Fields indicates the
-> FROM Students specific columns to
-> WHERE Age = ‘17’; be displayed.

ID Num LName FName MName


Age=‘17’ is the 003 Dela Cruz Pedro Ang
condition to be
satisfied. Only the 004 San Jose Juan Tan
rows with 17 as the 005 Santos Chad Moreno
age will be
displayed
USING WHERE CLAUSE
 Using Mysql Console:
mysql > SELECT ID Num, Lname, Fname, MName
-> FROM Students
-> WHERE Age = ‘17’; SIMPLE CONDITION

Comparison Operator Description


= Equal to
< Less than
> Greater than
<= Less than equal to
>= Greater than equal to
<> or != Not equal to
USING WHERE CLAUSE
List the last name & first name of student/s whose age is not 17 years old.

 Using Mysql Console: Fields indicates the


mysql > SELECT Lname, Fname, Age specific columns to
-> FROM Students be displayed.
-> WHERE Age <> ‘17’;

LName FName MName Age


Sison Carlo Sy 16
Age<>‘17’ is the Cruz Carla Uy 16
condition to be
satisfied. Only the Angeles Joan Reyes 16
rows with 17 as the Bauzon Janice Tan 18
age will be
Gomez Richard Cruz 22
displayed
Corpuz Jackie Montefalco 20
Bartolome Jade Corpuz 20
USING COMPOUND CONDITIONS
 Forming a compound conditions means connecting two or more simple conditions
with the use of AND, OR and NOT operators.

 AND Operator
- connects simple conditions wherein all must be TRUE in order for the
compound condition to be true.
 OR Operator
- connects simple conditions, the compound condition will be true
when any one of the simple condition is TRUE

 NOT Operator
- reverses the truth of the original condition.
USING AND OPERATOR FOR COMPOUND CONDITION
 List the ID number of student/s who are 17 yrs old and below whose gender is
male
ID number is the specific
 Using Mysql Console: column to be displayed
mysql > SELECT ID Num when the compound
-> FROM Students condition is true.
-> WHERE Age <= ‘17’ and Gender = ‘Male’;

The compound AND


condition to be satisfied in
order to display the ID
number of students.
OUTPUT OF AND OPERATOR FOR COMPOUND
CONDITION
 Using Mysql Console:
mysql > SELECT ID Num
-> FROM Students
-> WHERE Age <= ‘17’ and Gender = ‘Male’;

ID Num
001
003
004
005
USING OR OPERATOR FOR COMPOUND CONDITION
 List the first name of student/s who are 17 yrs old & above or whose gender is
female
Displays the first name of
 Using Mysql Console: students when the
mysql > SELECT FName compound OR condition is
-> FROM Students satisfied.
-> WHERE Age >= ‘17’ or Gender = ‘Female’;

Either of the simple


condition is TRUE, then the
compound OR condition is
true.
OUTPUT OF OR OPERATOR FOR COMPOUND
CONDITION
 Using Mysql Console:
mysql > SELECT FName
-> FROM Students
-> WHERE Age >= ‘17’ or Gender = ‘Female’;

FName Age Gender


Carla 16 Female
Pedro 17 Male
Juan 17 Male
Chad 17 Male
Joan 16 Female
Janice 18 Female
Richard 22 Male
Jackie 20 Female
Jade 20 Female
USING NOT OPERATOR FOR COMPOUND CONDITION
 Display the first name of student/s who are not male.

Displays the first name of


 Using Mysql Console: students when the
mysql > SELECT FName compound OR condition is
-> FROM Students satisfied.
-> WHERE Gender <> ‘Male’;

The condition will display


the opposite which is
Female.
OUTPUT OF NOT OPERATOR FOR COMPOUND
CONDITION
 Using Mysql Console:
mysql > SELECT FName
-> FROM Students
-> WHERE Gender <> ‘Male’;

FName
Carla
Joan
Janice
Jackie
Jade
USING THE BETWEEN OPERATOR
 The BETWEEN operator is inclusive, meaning that a value equal to either value in the
condition is selected.
Displays the customer
number, customer name
 Using Mysql Console:
and balance of a
mysql > SELECT Customer_Num, Customer_Name, Balance Customer.
-> FROM Customer
-> WHERE Balance BETWEEN 2,000 AND 5,000;

Values of either 2,000 or


5,000 would make the
condition true.
USING COMPUTED COLUMNS
 Computed columns does not exist in the database but can be used to compute using
data in the existing columns. Can involve any arithmetic operator.

 Below is the table for arithmetic operator.

Arithmetic Operator Description


+ Addition
- Subtraction
* Multiplication
/ Division
USING COMPUTED COLUMN
 Find the number, name and available credit for each customer with at least $5,000 of
available credit.
Displays the customer
 Using Mysql Console: number, customer name
mysql > SELECT Customer_Num, Customer_Name, (Credit-Balance) AS and available credit of a
-> AVAILABLE_CREDIT Customer.
-> FROM Customer
-> WHERE (CREDIT_LIMIT – BALANCE) >= $5,000;
Available_Credit is the
Computed Column

This condition is set to test


if the computed column
Available Credit is greater
than or equal to $5,000.
TABLE : CUSTOMER

Customer_Num Customer_Name Street City State Zip Balance Credit_Limit Rep_Num

148 Al's Appliance & Sport 2837 Greenway Fillmore FL 33336 $ 6,550.00 $ 7,500.00 20

282 Brookings Direct 3827 Devon Grove FL 33321 $ 431.50 $ 10,000.00 35

356 Ferguson's 382 Wildwood Northfield FL 33146 $ 5,785.00 $ 7,500.00 65

408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35

462 Bargains Galore 3829 Central Grove FL 33321 $ 3,412.00 $ 10,000.00 65

524 Kline's 838 Ridgeland Fillmore FL 33336 $ 12,762.00 $ 15,000.00 20


Johnson's Department
608 Store 372 Oxford Sheldon FL 33553 $ 2,106.00 $ 10,000.00 65

687 Lee's Sport & Appliance 282 Evergreen Altonville FL 32543 $ 2,851.00 $ 5,000.00 35

725 Deerfield's Four Seasons 282 Columbia Sheldon FL 33553 $ 248.00 $ 7,500.00 35

842 All Season 28 Lakeview Grove FL 33321 $ 8,221.00 $ 7,500.00 20

 Using Mysql Console:


mysql > SELECT Customer_Num, Customer_Name, (Credit-Balance) AS
-> AVAILABLE_CREDIT
-> FROM Customer
-> WHERE (CREDIT_LIMIT – BALANCE) >= $5,000;
 Output after executing the SQL Command

Customer_Num Customer_Name Available_Credit

148 Al's Appliance & Sport $ 950.00

282 Brookings Direct $ 9,568.50

356 Ferguson's $ 1,715.00

408 The Everything Shop $ (285.25)

462 Bargains Galore $ 6,588.00

524 Kline's $ 2,238.00

608 Johnson's Department Store $ 7,894.00

687 Lee's Sport & Appliance $ 2,149.00

725 Deerfield's Four Seasons $ 7,252.00

842 All Season $ (721.00)


USING THE LIKE OPERATOR
 The LIKE operator is used with a wildcard symbol to test for a pattern match.
 List the number, name and complete address of each customer located on a street that
contains the letters, “Central”
 Using Mysql Console:
mysql > SELECT Customer_Num, Customer_Name, Street
-> FROM Customer
-> WHERE Street LIKE ‘%Central%’
TABLE : CUSTOMER

Customer_Num Customer_Name Street City State Zip Balance Credit_Limit Rep_Num

148 Al's Appliance & Sport 2837 Greenway Fillmore FL 33336 $ 6,550.00 $ 7,500.00 20

282 Brookings Direct 3827 Devon Grove FL 33321 $ 431.50 $ 10,000.00 35

356 Ferguson's 382 Wildwood Northfield FL 33146 $ 5,785.00 $ 7,500.00 65

408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35

462 Bargains Galore 3829 Central Grove FL 33321 $ 3,412.00 $ 10,000.00 65

524 Kline's 838 Ridgeland Fillmore FL 33336 $ 12,762.00 $ 15,000.00 20


Johnson's Department
608 Store 372 Oxford Sheldon FL 33553 $ 2,106.00 $ 10,000.00 65

687 Lee's Sport & Appliance 282 Evergreen Altonville FL 32543 $ 2,851.00 $ 5,000.00 35

725 Deerfield's Four Seasons 282 Columbia Sheldon FL 33553 $ 248.00 $ 7,500.00 35

842 All Season 28 Lakeview Grove FL 33321 $ 8,221.00 $ 7,500.00 20


 Output after executing the SQL Command

Customer_Num Customer_Name Street


462 Bargains Galore 3829 Central
USING THE IN OPERATOR
 The IN clause (using IN operator) followed by a collection of values, provides a concise
way of phrasing certain conditions.

 The condition is true for those rows in which the value in the condition set is in the
collection.

 SYNTAX:
SELECT Field_1, Field_2, ... Field_x
FROM Tablename
WHERE Condition IN (Values / Collection);

 Using Mysql Console:


mysql > SELECT Customer_Num, Customer_Name, Credit_Limit
-> FROM Customer
-> WHERE Credit_Limit IN ($5,000, $10,000, $15,000)
 List the number, name and credit limit for each customer with a credit limit of
$5,000, $10,000 or $15,000
TABLE : CUSTOMER

Customer_Num Customer_Name Street City State Zip Balance Credit_Limit Rep_Num

148 Al's Appliance & Sport 2837 Greenway Fillmore FL 33336 $ 6,550.00 $ 7,500.00 20

282 Brookings Direct 3827 Devon Grove FL 33321 $ 431.50 $ 10,000.00 35

356 Ferguson's 382 Wildwood Northfield FL 33146 $ 5,785.00 $ 7,500.00 65

408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35

462 Bargains Galore 3829 Central Grove FL 33321 $ 3,412.00 $ 10,000.00 65

524 Kline's 838 Ridgeland Fillmore FL 33336 $ 12,762.00 $ 15,000.00 20


Johnson's Department
608 Store 372 Oxford Sheldon FL 33553 $ 2,106.00 $ 10,000.00 65

687 Lee's Sport & Appliance 282 Evergreen Altonville FL 32543 $ 2,851.00 $ 5,000.00 35

725 Deerfield's Four Seasons 282 Columbia Sheldon FL 33553 $ 248.00 $ 7,500.00 35

842 All Season 28 Lakeview Grove FL 33321 $ 8,221.00 $ 7,500.00 20

 Using Mysql Console:


mysql > SELECT Customer_Num, Customer_Name, Credit_Limit
-> FROM Customer
-> WHERE Credit_Limit IN ($5,000, $10,000, $15,000)
 Output after executing the SQL Command

Customer_Num Customer_Name Credit_Limit


282 Brookings Direct $ 10,000.00
408 The Everything Shop $ 5,000.00
462 Bargains Galore $ 10,000.00
524 Kline's $ 15,000.00
608 Johnson's Department Store $ 10,000.00
687 Lee's Sport & Appliance $ 5,000.00
USING THE ORDER BY CLAUSE
 The ORDER BY clause is used to list data in a specific order.

 SYNTAX:
SELECT Field_1, Field_2, ... Field_x
FROM Tablename
ORDER BY sort key;
Displays the customer
 Using Mysql Console: number, customer name
and balance of a
mysql > SELECT Customer_Num, Customer_Name, Balance
Customer.
-> FROM Customer
-> ORDER BY Balance; Sort key
 List the number, name and balance of each customer. Sort the output in
ascending order by balance.

 Using Mysql Console:


mysql > SELECT Customer_Num, Customer_Name, Balance
-> FROM Customer
-> ORDER BY Balance;

Customer_Num Customer_Name Balance


725 Deerfield's Four Seasons $ 248.00
282 Brookings Direct $ 431.50
608 Johnson's Department Store $ 2,106.00
687 Lee's Sport & Appliance $ 2,851.00
462 Bargains Galore $ 3,412.00 Rows are sorted in
408 The Everything Shop $ 5,285.25 increasing order by
356 Ferguson's $ 5,785.00 balance
148 Al's Appliance & Sport $ 6,550.00
842 All Season $ 8,221.00
524 Kline's $ 12,762.00
USING THE ORDER BY CLAUSE with MULTIPLE SORT
KEYS
 List the number, name and balance of each customer. Sort the customer by name
within descending credit limit.

 Using Mysql Console:


mysql > SELECT Customer_Num, Customer_Name, Credit_Limit
-> FROM Customer
-> ORDER BY Credit_Limit DESC, Customer_Name;
USING THE ORDER BY CLAUSE with MULTIPLE SORT
KEYS
 Using Mysql Console:
mysql > SELECT Customer_Num, Customer_Name, Credit_Limit
-> FROM Customer
-> ORDER BY Credit_Limit DESC, Customer_Name;

Customer_Num Customer_Name Credit_Limit


524 Kline's $ 15,000.00
462 Bargains Galore $ 10,000.00
282 Brookings Direct $ 10,000.00
608 Johnson's Department Store $ 10,000.00
148 Al's Appliance & Sport $ 7,500.00
842 All Season $ 7,500.00
725 Deerfield's Four Seasons $ 7,500.00
356 Ferguson's $ 7,500.00
687 Lee's Sport & Appliance $ 5,000.00
408 The Everything Shop $ 5,000.00
USING FUNCTIONS
 Applies to groups of rows.
 Can be applied to all the rows in a table.
 Could apply to those rows satisfying some particular condition

MySQL Aggregate Functions

Function Description
AVG Calculates the average value in a column
COUNT Determines the number of rows in a table
MAX Determines the maximum value in a column
MIN Determines the minimum value in a column
SUM Calculates a total of the values in a column
USING THE COUNT FUNCTION
 This function is used to count the number of ROWS in a table / relation.

 SYNTAX:
SELECT COUNT (*)
FROM Tablename
WHERE Condition;

COUNT (*) – to represent any column from the relation


COUNT (specific attribute) – specific column where counting will take place.
Condition has to be enclosed in a single quote.
TABLE : PART

Part_Num Description On_Hand Class Warehouse Price

AT94 Iron 50 HW 3 $ 24.95

BV06 Home Gym 45 SG 2 $ 794.95

CD52 Microware Oven 32 AP 1 $ 165.00

DL71 Cordless Drill 21 HW 3 $ 129.95

DR93 Gas Range 8 AP 2 $ 495.00

DW11 Washer 12 AP 3 $ 399.99

FD21 Stand Mixer 22 HW 3 $ 159.95

KL62 Dryer 12 AP 1 $ 349.95

KT03 Dishwasher 8 AP 3 $ 595.00

KV29 Treadmill 9 SG 2 $ 1,390.00


USING THE COUNT FUNCTION
 How many parts are there in item class HW?

 Using Mysql Console:


mysql > SELECT COUNT (*)
-> FROM PART
-> WHERE Class = ‘HW’;

 Output after executing the command

Count (*)
3
USING THE SUM FUNCTION
 This function is used if you want to total the values in a given attribute.

 SYNTAX:
SELECT SUM (attribute name to be totaled)
FROM Tablename;

SUM (attribute name to be totaled ) – specifies the column where the total will
be computed.
TABLE : CUSTOMER

Customer_Num Customer_Name Street City State Zip Balance Credit_Limit Rep_Num


Al's Appliance &
148 Sport 2837 Greenway Fillmore FL 33336 $ 6,550.00 $ 7,500.00 20

282 Brookings Direct 3827 Devon Grove FL 33321 $ 431.50 $ 10,000.00 35

356 Ferguson's 382 Wildwood Northfield FL 33146 $ 5,785.00 $ 7,500.00 65

408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35

462 Bargains Galore 3829 Central Grove FL 33321 $ 3,412.00 $ 10,000.00 65

524 Kline's 838 Ridgeland Fillmore FL 33336 $ 12,762.00 $ 15,000.00 20


Johnson's
608 Department Store 372 Oxford Sheldon FL 33553 $ 2,106.00 $ 10,000.00 65
Lee's Sport &
687 Appliance 282 Evergreen Altonville FL 32543 $ 2,851.00 $ 5,000.00 35
Deerfield's Four
725 Seasons 282 Columbia Sheldon FL 33553 $ 248.00 $ 7,500.00 35

842 All Season 28 Lakeview Grove FL 33321 $ 8,221.00 $ 7,500.00 20


USING THE SUM FUNCTION
 Find the total number of Premiere products customers and the total of their balances.

 Using Mysql Console:


mysql > SELECT COUNT (*), SUM(Balance)
-> FROM CUSTOMER;

 Output after executing the command

Count (*) SUM(Balance)


10 47,651.75
USING THE AVG, MAX & MIN FUNCTION
 This function AVG is used calculate the average value in a numeric range.
 SYNTAX:
SELECT AVG (attribute name to be calculated)
FROM Tablename;

 MAX is used to calculate the maximum value in a numeric range.

 SYNTAX:
SELECT MAX (attribute name to be calculated)
FROM Tablename;

 MIN is used to calculate the minimum

 SYNTAX:
SELECT MIN (attribute name to be calculated)
FROM Tablename;

You might also like