Lesson07-Data Query Language
Lesson07-Data Query Language
SQL is equipped with data Query Language (DQL) that is used to retrieve data from a database.
SELECT – This clause selects the attributes based on the condition described by WHERE
clause.
FROM − This clause takes a table name as an argument from which attributes are to be
selected/projected. In case more than one table names are given, this clause corresponds to
Cartesian product.
WHERE − This clause defines predicates or conditions, which must match in order to
qualify the attributes to be projected.
For example −
SELECT stud_name FROM students WHERE county_id = 47;
This command will yield the names of students from the relation students whose home county is
Nairobi (47).
The output of a SELECT statement is called a result set. That is, a set of data that results from a
query.
The bikeshop database shall be provided as MySQL file (bikeshop.sql) and will be used to demonstrate
the concepts to be covered in the subsequent lessons. It contains several tables that will be used to learn
database and query concepts. Below is an ER diagram that summarizes the schema of the bikeshop
database
1
The following queries uses the SELECT statement to;
NB: Be careful on using the SELECT * queries in your applications. A good practice is to
only select the columns that you intend to get data from. This is because;
The SELECT *, by returning data from columns that you may not use, generates unnecessary
I/O disk and network traffic between the MySQL database server and application.
The result set of SELECT * is not predictable because if someone changes the table by adding
more columns, the result set changes to a different one from the one that you expected.
SELECT * may expose sensitive information to unauthorized users.
2
i. find all products whose model year is 2017:
SELECT product_name FROM products WHERE model_year=2017;
ii. find all products whose model year is 2017 and price is 999.99:
SELECT product_name FROM products
WHERE model_year=2017 AND list_price=999.99;
iii. find all products whose price is greater than 1500:
... WHERE list_price>1500;
iv. find all products whose price is 999.99 or price is 1999.99:
... WHERE list_price=999.99 OR list_price=1999.99;
v. find all products whose price is between 1000 and 2000:
... WHERE list_price BETWEEN 1000 AND 2000;
vi. find the names and emails of staff members whose last name ends with the string ‘rez':
SELECT first_name, last_name, email FROM customers
WHERE last_name like '%rez';
vii. find the names and emails of staff members whose last name begins with the string ‘mar’:
... WHERE last_name like 'mar%';
viii. find the names and emails of staff members whose last name contains the string ‘oul’:
... WHERE last_name like '%oul%';
ix. find all products whose brand_id is either of the following: 3, 5, 7,9:
... WHERE brand_id IN (3,5,7,9);
x. find the names and emails of customers without phone numbers:
... WHERE phone IS NULL;
The result set of a SELECT statement is usually not sorted – implying that the rows in the result set
can be in any order. The following queries uses the ORDER BY clause to
i. find product_name, price, and brand_id of products whose model year is 2017 sorted by
price:
SELECT product_name, list_price, brand_id FROM products
WHERE model_year=2017
ORDER BY list_price;
ii. find … of products whose model year is 2017 sorted by price, and then by brand_id:
... ORDER BY list_price ASC , brand_id ASC;
The result set can be sorted in ascending or in descending order. By default, the result set is implicitly
sorted in ascending order (ASC). To change the order, specify the DESC option. Eg.
3
... ORDER BY list_price DESC, brand_id ASC;
Using our case study “bikeshop” database, try out the SQL commands discussed in section 5.2
above.
While still at it, try out the other DML commands (Insert, Update , and Delete)