0% found this document useful (0 votes)
15 views

Lab 6-Queries (Using SQL)

This document provides instructions for writing SQL queries manually without using a query wizard. It explains the basic components of a SQL query and demonstrates examples of selecting data from single and multiple tables. Sample queries are provided to retrieve various data fields and filter results based on conditions.

Uploaded by

jianmgamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab 6-Queries (Using SQL)

This document provides instructions for writing SQL queries manually without using a query wizard. It explains the basic components of a SQL query and demonstrates examples of selecting data from single and multiple tables. Sample queries are provided to retrieve various data fields and filter results based on conditions.

Uploaded by

jianmgamal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

23CSIS01P

Introduction to Information Systems

Lab (6)

QUERIES (USING SQL)


23CSIS01P – Introduction to Information Systems Lab sheet (6)

Lab (6)
In our previous lab, we gave an idea of what queries are and why they are used. We gave examples on
the different types of queries that can be used and we created them using the query wizard. In this lab,
we will learn how to manually write queries using SQL.

What is SQL?
Queries are written in a language called SQL, which stands for Structured Query Language. As we
mentioned in the previous lab, data can be retrieved from one or more tables.

Query format
A query typically consists of three statements. The first two are mandatory while the third is optional.
SELECT < Attribute List >
FROM < Table List >
WHERE < Condition >

Essentially the idea is that we want to Select one or more attributes from a specific table or set of tables.
It is possible to add one or more conditions to restrict the output to something specific

SELECT statement: this is where we write the attribute(s) we want to retrieve. If we wish to retrieve
more than one attribute; we simply separate them with commas.

FROM statement: after we've specified which attribute(s) we want to retrieve, we must specify which
table or tables that the attribute(s) come from.

WHERE statement: This statement is added when there is a specific condition.

1
23CSIS01P – Introduction to Information Systems Lab sheet (6)

For this lab, we will be using the schema that was created in the previous lab which was for a company.
To recap, the relationships and data are included below:

Note: There are 3 main tables (also called relations) and a 1:M relationship between Department and
Employee as well as a M:N relationship between Employee and Project

Employee table:

Department table:

2
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Project table:

Employee-Project table:

Steps to write an SQL statement with ACCESS

Step 1:

Go to the Create tab, and choose Query Design

3
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Step 2:
The Query Wizard display will appear, including the pop-up window from which you choose the
tables you want to perform a query on. HOWEVER, we will not be using any wizard this time. Close
the pop-up window without choosing any tables.

Step 3:
Right-click on the Query tab and choose SQL View. A blank white space will appear and this is where
we write SQL statements.

4
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Step 4 - Part I:
As we proceeded in the previous lab, we will first start by retrieving basic information from only one
table. A number of keywords and ideas will be introduced and all of them can be used when retrieving
from more than one table. After writing the SQL code, click RUN (!)

Query 1:
Retrieve the Last Name of all the Employees.

Code:

SELECT Lname

FROM Employee;

Results:

Query 2:
Retrieve the First and Last Names of all the Employees born before 1990.

Code:

SELECT Fname, Lname

FROM Employee

WHERE DateOfBirth < #1/1/1990#;

5
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Results:

Query 3:
Retrieve the First and Last Name of all the Employees whose first name starts with 'A'.

Note:

Whenever we want to retrieve data that contains certain words or letters, this is called "String
matching". The keyword for string matching in SQL is LIKE. It checks for an exact match of the text
and it is not case sensitive.

Code:

SELECT Fname, Lname

FROM Employee

WHERE Fname LIKE "A*";

Results:

6
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Query 4:
Retrieve the Average salary and Maximum salary that the employees take.

Make sure that your results are clearly labeled.

This query is actually composed of 2 parts. The first part involves the use of aggregate functions
while the second part involves some editing to the output being displayed. We will first provide the
code for the first part only.

Code: Results:

SELECT AVG(Salary), Max(Salary)

FROM Employee;

Now, in the above screenshot, you will notice that the column headers for the 2 values are Expr1000
and Expr1001. A user who sees this output will not be able to determine which value is the average
and which value is the maximum, which brings us to the second part of the query.

We now want to edit the output to tell us which value is the maximum and which value is the
average. In order to do so, we use what is known as an Alias. An alias is like a fake name that is only
displayed to the user and is NOT written or stored in the database. They keyword used is "AS" and the
new name is included between single quotes.

Code:

SELECT AVG (Salary) AS 'Average Salary', Max (Salary) AS


'Maximum Salary' FROM

Employee;

Final Results:

7
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Query 5:
Retrieve all the details of every Employee, and arrange the results alphabetically by first name.

This query is also divided into 2 parts. First is the idea of selecting all the attributes for a specific
table. In order to do that, we simply use the asterisk (*) which means ALL. Now to arrange the output,
we use the keyword ORDER BY. We can either display in ascending (alphabetical) or descending
(Reverse alphabetical) order.
By default when using the ORDER BY keyword, the data is arranged in ascending order. If we
want it in descending order, we simply write DESC after the attribute name.

Code:

SELECT *

FROM Employee

ORDER BY Fname;

Results:

Step 4 - Part II:


We now move on to more complicated queries, where we will retrieve information from 2 tables. It
is important to note that you can retrieve information from even more than 2 tables as the concept
remains the same.

8
23CSIS01P – Introduction to Information Systems Lab sheet (6)

The idea is to use the connection between the tables which is the relationship between them. As we
had previously discussed, the relationship is represented through the use of foreign keys. So, when
retrieving information from 2 or more tables, we need to make sure that there is a condition to check
that the primary key of one table is equal in value to the foreign key of the related table. The following
is an example of such a situation:

Query 6:
Retrieve the first and last names of all the Employees along with the name of the department that
they work in.

We will approach the question in a logical and sequential way. First, we list the attributes that we
want to select which are: Fname, Lname and Dname. Then we specify the tables where those
attributes belong which are: Employee and Department.
Now we want to make sure that those tables are connected, so we use a condition where we check
that the department number in the department table (PK) is equal in value to the department number in
the Employee table (FK).

Code:

SELECT Fname, Lname, Dname

FROM Employee, Department

WHERE Employee.Dnum = Department.Dnumber;

Results:

9
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Important Notes:

✓ You can only retrieve data from tables that are either connected to each other (1:M relationship)
or connected to each other through another table (M:N relationship). If the tables are not
connected, you can't get data from both of them within the same query.

✓ If you want to ensure that your results do not contain repeated records, use the keyword
DISTINCT. As an example:
o Code:
▪ SELECT DISTINCT address FROM Employee o Result:
▪ Without the DISTINCT keyword

▪ Using the DISNTINCT keyword

10
23CSIS01P – Introduction to Information Systems Lab sheet (6)

Do It Yourself
1. Retrieve all Employees born between 1960 and 1990.
2. Retrieve the Minimum salary for the Employees.
3. Retrieve Employees' IDs, first and last names but arrange results according to ID in descending
order.
4. Retrieve the Employees' first name, salary, and his/her department location.

5. Add an attribute to the Project table called ‘Budget’. Write a query to retrieve the names of
projects, as well as the number of employees in each project, provided that the projects have a
budget more than 3000.

11

You might also like