Professional Documents
Culture Documents
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
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’;
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’;
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;
148 Al's Appliance & Sport 2837 Greenway Fillmore FL 33336 $ 6,550.00 $ 7,500.00 20
408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35
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
148 Al's Appliance & Sport 2837 Greenway Fillmore FL 33336 $ 6,550.00 $ 7,500.00 20
408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35
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
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);
148 Al's Appliance & Sport 2837 Greenway Fillmore FL 33336 $ 6,550.00 $ 7,500.00 20
408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35
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
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.
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 (*)
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
408 The Everything Shop 1828 Raven Crystal FL 33503 $ 5,285.25 $ 5,000.00 35
SYNTAX:
SELECT MAX (attribute name to be calculated)
FROM Tablename;
SYNTAX:
SELECT MIN (attribute name to be calculated)
FROM Tablename;