In the lab we learn the following concepts used in SELECT Query of SQL.
Distinct keyword before the column(s) eliminate duplicate values and return only different values.
Task: On DreamHome database run the following queries:
Select city from Branch;
Select distinct(city) from Branch
The first query returns duplicate city, while the second one returns only a distinct set of values.
Provides a way to give a different name to a table, column or a derived column in the results. Note that name are only applicable for results, they do not affect the metadata of the tables.
Task: On DreamHome database run the following queries:
Select city from Branch;
Select city As MyCity from Branch;
Select staffNo as ID, fName as FirstName, lName as [Last Name], position as Position, sex as Gender, DOB as [Date of Birth], salaray as Income from Staff;
Select (fName +' '+lName) as [Name], position as Position from Staff;
Click Me to open the SQL Script file of this lab.
Filtering of Data based on single and compound conditions You can select data of your choice from all rows of a table by specifying a condition using a Where clause in the SELECT Command.
The five basic search conditions:
In SQL, the following simple comparison operators are available:
More complex predicates can be generated using the logical operators AND, OR, and NOT, with parentheses (if needed or desired) to show the order of evaluation.
The rules for evaluating a conditional expression are:
Task: On DreamHome database run the following queries and note your results. More queries will be asked in the class and you will provide the solutions:
SELECT staffNo, fName, lName, position, salary FROM Staff WHERE salary >10000;
SELECT * FROM Branch WHERE city ='London' OR city='Glasgow';
SELECT staffNo, fName, lName, position, salary FROM Staff
WHERE salary BETWEEN 20000 AND 30000;
SELECT staffNo, fName, lName, position, salary FROM Staff
WHERE salary >= 20000 AND salary <=30000;
SELECT staffNo, fName, lName, position FROM Staff WHERE position IN (‘Manager’, ‘Supervisor’);
SELECT staffNo, fName, lName, position FROM Staff WHERE position =‘Manager’ OR position =‘Supervisor’;
SQL has two special pattern-matching symbols:
Find all owners with the string "Glasgow" in their address.
SELECT ownerNo, fName, lName, address, telNo FROM PrivateOwner WHERE address LIKE ‘%Glasgow%’;
List the details of all viewings on property PG4 where a comment has not been sup-plied.
SELECT clientNo, viewDate FROM Viewing WHERE propertyNo = ’PG4’ AND comment IS NULL
You can read data from multiple tables by separating table name by a comma ",". Note that if you do not specify a condition then results will be a cross multiplication of tables and may results into invalid rows. Therefore, to generate meaningful information we should have some condition among the tables.
Task: On DreamHome database run the following queries:
Select * from Branch,Staff;
Select * from Branch,Staff Where Branch.banchNo = Staff.branchNo;
Select * from Branch as b,Staff as s Where b.banchNo = s.branchNo;
Select * from Branch as b,Staff as s,PropertyForRent as pfr
Where b.banchNo = Staff.branchNo
And b.banchNo = pfr.branchNo;
Select * from Branch as b,Staff as s, PropertyForRent as pfr
Where b.banchNo = Staff.branchNo
And b.banchNo = pfr.branchNo
And s.staffNo = pfr.staffNo;
ORDER BY is used with SELECT Query to sort the results in ascending or descending order.
Task: On DreamHome database run the following queries:
Select city from Branch order by city;
Select distinct(city) from Branch order by city desc;
To earn PASS status, system is about to evaluate you using multiple choice questions (MCQs) and True/False type questions.
Topics |
|