Department of Computer Science & Information Technology
Khwaja Fareed University of Engineering & Information Technology
Course: Database Systems
Lab Instructor:
Ms. Hira Kanwal
Student Name
Student Roll #
Department
Batch/Year/Section
For Lab. Instructor
Marks Signature
KFUEIT Department of CS/IT
lxiii
Lab Manual # 8 Creating & Manipulating Databases
8.1. Objective
1. Querying Multiple Table
2. Table Aliases
3. Joins
4. Inner Join
5. Adding Additional Clauses
8.2. Querying Multiple Table
Sometimes we may need to use data from more than one table. For example, to select data
from two separate tables: First Name from Employees table, Department Name from
Departments table.
Multiple tables (two or more tables) can be linked only if they have common values (in this
case, department number) or a logical connection of some kind.
8.3. Table Aliases
In joining two or more table, we have to write table name in front of every column name.
This qualifying of column names with table names can be time consuming, and may result in
a very long, unreadable query.
In SQL Server, instead of writing full table name after each column, we can use Table
Aliases. Just as Column Alias gives a column another name, a table alias gives a table another
name.
For example:
SELECT emp.last_name , [Link] , dep.department_name
FROM employees emp JOIN departments dep
ON emp.department_id = dep.department_id
Notes:
KFUEIT Department of CS/IT
64
Lab Manual # 8 Creating & Manipulating Databases
1. Table aliases are defined in the FROM clause.
2. We can assign any alias to a table (for example, you can assign the letter A to the
Employees table); however, it is advisable to assign meaningful aliases.
3. The table alias is valid only for the current SQL Server SELECT statement.
8.4. Joins
Here are the different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Return all records from the left table, and the matched
records from the right table
RIGHT (OUTER) JOIN: Return all records from the right table, and the matched
records from the left table
FULL (OUTER) JOIN: Return all records when there is a match in either left or
right table
8.5. Inner Join OR Simple Join
Inner JOIN statement allows us to retrieve rows from both tables only if they match the join
condition. An SQL JOIN clause is used to combine rows from two or more tables, based on a
common field between them.
INNER JOIN is the same as JOIN; the keyword INNER is optional.
Below is the example of SQL Inner Join or Simple Join.
Example 1:
Suppose there are two tables Student(StdId, StdName, StdAddress, StdContact)
StudentMark(StdId, StdGPA). We want to show the complete detail of Student from both the
tables.
KFUEIT Department of CS/IT
65
Lab Manual # 8 Creating & Manipulating Databases
select s.*, sm.*
from Student s inner join StudentMark sm
ON [Link] = [Link]
Example 2:
Suppose we have two tables as shown below, we want to write a query that give details of
both tables grouped together.
select [Link], [Link], [Link], sum([Link]) as TotalCustomer,
sum([Link]) as Income, sum([Link]) as Expenditure
from branch a inner join branchdetail b
ON [Link] = [Link]
group by [Link],[Link],[Link]
Order by [Link]
Points to Note:
1. In the Select clause, always precede the column name with table name for clarity.
2. When a column is common to both tables, it must be prefixed with table name.
3. In the From clause, specify the tables from which you would like to retrieve the data.
The tables are specified with the keyword JOIN between them.
4. After the ON keyword, specify the Join condition.
5. This relation involves primary key and foreign key.
8.6. Adding Additional Clauses
We can add additional clauses to SELECT statement. For example:
This SQL Server example would retrieve all employees whose salary is greater than 6000 and
their department number equals 90, and orders them according to their salary:
SELECT emp.last_name , [Link] , dep.department_name
FROM employees emp JOIN departments dep
ON emp.department_id = dep.department_id
WHERE emp.department_id = 90
ORDER BY [Link] DESC
KFUEIT Department of CS/IT
66
Lab Manual # 8 Creating & Manipulating Databases
8.7. Joining More than Two Tables
Sometimes we may need to join more than two tables. For example: displaying the
employee’s name from the Employee table, the name of the department where the
employee works from the Departments table, and the name of the region where this
department resides from the Regions table.
Joining an additional table requires us to:
1. Specify the table name in the SQL Server FROM clause (by using an additional JOIN
keyword).
2. Determine the additional join condition, and specify this condition by using additional
ON keyword.
SELECT emp.last_name , [Link] , dep.department_name, [Link]
FROM employees emp JOIN departments dep
ON emp.department_id = dep.department_id
JOIN regions reg
ON dep.region_id = reg.region_id
The same concept applies to joining four tables or more – adding the table name after the
JOIN keyword, and specifying additional join condition after the ON keyword.
8.8. Left Join
This join returns all the rows of the table on the left side of the join and matching rows for the
table on the right side of join. The rows for which there is no matching row on right side, the
result-set will contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
8.9. Right Join
RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the right
side of the join and matching rows for the table on the left side of join. The rows for which
there is no matching row on left side, the result-set will contain null. RIGHT JOIN is also
known as RIGHT OUTER JOIN.
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
KFUEIT Department of CS/IT
67
Lab Manual # 8 Creating & Manipulating Databases
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
8.10. FULL JOIN
FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT
JOIN. The result-set will contain all the rows from both the tables. The rows for which there
is no matching, the result-set will contain NULL values.
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
8.11. LAB TASKS
8.11.1. Customers and internet packages (Customers & Packages tables) –
[Link]. Write a query to display first name, last name, package number and internet
speed for all customers.
[Link]. Write a query to display first name, last name, package number and internet
speed for all customers whose package number equals 22 or 27. Order the query in
ascending order by last name.
KFUEIT Department of CS/IT
68
Lab Manual # 8 Creating & Manipulating Databases
8.11.2. Display the package number, internet speed, monthly payment and sector name for all
packages (Packages and Sectors tables).
8.11.3. Display the customer name, package number, internet speed, monthly payment and
sector name for all customers (Customers, Packages and Sectors tables).
8.11.4. Display the customer name, package number, internet speed, monthly payment and
sector name for all customers in the business sector (Customers, Packages and Sectors
tables).
KFUEIT Department of CS/IT
69
Lab Manual # 8 Creating & Manipulating Databases
8.11.5. Display the last name, first name, join date, package number, internet speed and
sector name for all customers in the private sector who joined the company in the year
2006. (Customers, Packages and Sectors tables)
8.11.6. Customers and internet packages (Customers and Packages tables)
[Link]. Display the first name, last name, internet speed and monthly payment for all
customers. Use INNER JOIN
[Link]. Modify last query to display all customers, including those without any
internet package.
KFUEIT Department of CS/IT
70
Lab Manual # 8 Creating & Manipulating Databases
[Link]. Modify last query to display all packages, including those without any
customers.
[Link]. Modify last query to display all packages and all customers.
KFUEIT Department of CS/IT
71