RDMBS-MySQL-Lesson-6
RDMBS-MySQL-Lesson-6
MANAGEMENT SYSTEM
Introduction to MySQL
Part 2
THINGS TO LEARN:
■ Select statement
■ Where clause
■ Sorting Data
– Order By clause
■ SQL Functions
■ Single Row Functions
Basic SELECT statement
■ Data Retrieval using the SQL SELECT statement
– The SELECT statement is used to retrieve records/rows from one or more
tables.
– consists of at least the SELECT clause and the FROM clause.
– Syntax:
■ SELECT <select_expression> FROM <table_name>;
– select_expression is column name / comma-separated list of columns that
appears in the query result
– table_name is the name of the source table
Basic SELECT statement
Note: that world is the source schema used in all SQL statements presented.
■ Information requirement: List of all cities along with their respective populations.
– SELECT name, population FROM city;
■ result:
Basic SELECT statement
■ Information requirement: List of all countries, their populations and their projected
populations if these increase by 25%.
– SELECT name country, population, population*1. 25 AS “projected population”
FROM country;
■ result:
Basic SELECT statement
■ Special operators:
– LIKE evaluates to TRUE if a value matches a specified value. Utilizes the % and
_ characters. (e.g. „%range%‟, „_nd%‟, „at%‟)
– IN ( ) returns TRUE if a value matches any value in the list / set enclosed in
parentheses
– BETWEEN returns a range of values
– IS NULL is used to check if a value is NULL or not
– EXISTS returns TRUE if the value exists
WHERE clause
■ Information requirement: List of all cities with populations below 5000 and whose
names end with “Island”.
– SELECT name city, district FROM city WHERE population < 5000 AND district
LIKE '%Island’;
■ result:
■ Information requirement: List of all cities that either have populations below 5000 or
whose names end with “Island”.
– SELECT name city, district FROM city WHERE population < 5000 OR district
LIKE '%Island’;
■ result:
■ Information requirement: List of all cities whose populations range from 5,000 to
10,000. Include the population in the list.
– SELECT name city, population FROM city WHERE population BETWEEN 5000
AND 10000;
■ result:
Sorting data: ORDER BY clause
■ Syntax:
– SELECT <select_expression> FROM <table_name> ORDER BY
<col_name/col_alias/col_number> [ASC/DESC]
[,col_name/col_alias/col_number> [ASC/DESC] [, …] ;
where:
– col_name : the name of the column
– col_alias : the column alias
– col_number : the column‟s order number in the select list
Sorting data: ORDER BY clause
■ Information requirement: List of all cities along with their respective populations.
Sort the data according to population.
– SELECT name, population FROM city ORDER BY population;
– SELECT name, population FROM city ORDER BY 2;
■ result:
Sorting data: ORDER BY clause
■ Single-row functions
– operate on single rows and return one result per row
– the function argument may be a column or an expression
■ the expression may contain another function, thus, making it possible to nest
functions
– manipulate different types of data and provide for the conversion from one
data type to another
Single-row functions
■ Information requirement: List of all Asian countries and their respective population
density, rounded up to integer values.
– SELECT Name, ROUND(Population/SurfaceArea,0) AS "Population Density“
FROM country WHERE continent = "Asia"
■ Result
Group functions
COUNT(*) returns ALL rows. The 2nd query returns the same
value - the column name has no nulls. The 3rd query returns
a lower value; null indepyear values are not counted.
Group functions
No rows are null; all rows, even with 0 values, are included in
the sum: average = sum of LifeExpectancy values / number
of all rows
GROUP BY clause
■ The LIMIT clause is used to specify the number of rows to be returned by the query
based on the sort order specified (or implied) in the query.
– SELECT * FROM city LIMIT 10;
■ The query returns the first 10 rows from the city table based on the default order.
– SELECT * FROM city LIMIT 1,15;
■ The query returns 15 rows from the top.
LIMIT clause
■ Information requirement: Alphabetical list of all cities in the Philippines. Display only
the first 15 rows.
– SELECT name FROM city WHERE countrycode = ‘PHL’ ORDER BY 1 LIMIT 15;
■ Result:
LIMIT clause
■ SELECT * FROM city WHERE countrycode = ‘PHL’ ORDER BY name LIMIT 16,20;
– The query returns the next 20 rows, starting from the 16th row in the list.
TOP-N Queries
■ Information requirement: List of the top 5 Asian countries with the highest
populations.
– SELECT code, name, continent, population FROM country ORDER BY
population DESC LIMIT 5;
■ Result:
TOP-N Queries