Vous êtes sur la page 1sur 6

Database and Management System

LAB JOURNAL # 14

Program: Software Engineering


Class: BSE-4A
Submitted By: Mujeeb Ijaz
Enrolment No: 01-131162-022
Submitted To: Ma’am Iqra Basharat

BAHRIA UNIVERSITY ISLAMABAD CAMPUS

Mujeeb Ijaz 01-131162-022


Lab # 14
MS SQL Server

Objectives:
 SUB QUERIES
 JOINS
 Left Outer Join
 Right Outer Join
 Full Outer Join

Toot Used:
Visual Studio 2013

Submission Date
25th May 2018

Evaluation: Signatures of Teacher

Mujeeb Ijaz 01-131162-022


Sub Query
• Sub queries are queries embedded in queries.
• They are used to retrieve data from one table based on data in another table.
• They generally are used when tables have some kind of relationship. The sub query can
contain any valid SELECT statement, but it must return a single column with the expected
number of results.
• For example, if the sub query returns only one result, then the main query can check for
equality, inequality, greater than, less than, etc.
• On the other hand, if the sub query returns more than one record, the main query must check
to see if a field value is (or is NOT) IN the set of values returned.

Table Alias
• Using full table names as prefixes can make SQL queries unnecessarily wordy.
• Table aliases can make the code a little more concise.
• An alias can be called whatever you want.
• Though typically it's the first letter(s) of the table name, it can be whatever makes sense to
you as the Developer.
• For example, the alias for a table called Courses can be c, or crs, or debbie, etc.

Left Join
• A LEFT JOIN (also called a LEFT OUTER JOIN) returns all the records from the first table even
if there are no matches in the second table.
• All rows in table1 will be returned even if they do not have matches in table2.

Right Join
• A RIGHT JOIN (also called a RIGHT OUTER JOIN) returns all the records from the second table
even if there are no matches in the first table.

Full Join
• A FULL JOIN (also called a FULL OUTER JOIN) returns all the records from each table even if
there are no matches in the joined table.

Lab Task
1. Find the name of the company that placed order 10290.

SELECT CompanyName
FROM Customers
WHERE CustomerID = (SELECT CustomerID FROM Orders WHERE OrderID = 10290);

Mujeeb Ijaz 01-131162-022


2. Find the Companies that placed orders in 1997.

SELECT CompanyName FROM Customers


WHERE CustomerID IN (SELECT CustomerID FROM Orders WHERE OrderDate BETWEEN '1997-01-01'
AND '1997-12-31');

3. Write a query that shows all products by name that are in the Seafood category.

SELECT ProductName from Products where CategoryID = (select categoryID from Categories where
CategoryName='Seafood');

4. Write a query that shows all companies by name that sell products in CategoryID 8.

Select CompanyName from Suppliers where SupplierID IN ( Select SupplierID from Products where
CategoryID = 8);

5. Write a query that shows all companies by name that sell products in the Seafood
category.
Select CompanyName from Suppliers where SupplierID IN ( Select SupplierID from Products where
CategoryID IN (select CategoryID from Categories where CategoryName = 'seafood'));

Mujeeb Ijaz 01-131162-022


Join
1. Write a query showing employee orders.

select Employees.FirstName, Orders.OrderID from Employees left join orders ON


(Employees.EmployeeID=Orders.EmployeeID);

2. Write a query that shows the order ids and the associated employee names for orders
that shipped after the required date.

select Orders.OrderID, Employees.FirstName from orders left join Employees ON


(Orders.EmployeeID=Employees.EmployeeID) where (RequiredDate < ShippedDate);

3. Write a query that shows the total quantity of products (from the Order_Details
table) ordered. Only show records for products for which the quantity ordered is
fewer than 200.

select sum(Quantity) from [Order Details] where (Quantity <200) group by OrderID;

Mujeeb Ijaz 01-131162-022


4. Write a query that shows the total number of orders by Customer since December
31, 1996. The report should only return rows for which the NumOrders is greater
than 15. The report should return the following 5 rows.

SELECT c.companyname,count(o.orderid) FROM orders o join customers c on(o.customerid=


c.customerid) WHERE o.orderdate >= '31-dec-1996'
GROUP by c.companyname having count(o.orderid)>15
ORDER by count(o.orderid)

5. Create a report that shows the number of employees and customers from each city
that has employees in it.

SELECT count(distinct(e.employeeid))as Numberemp,count(distinct(c.customerid)) as Numcus,e.city


FROM employees e join customers c on(e.city=c.city)
GROUP by e.city,c.city order by Numberemp

Conclusion

In this lab, we learn about the sub queries and joins. We implement some task related to sub
queries which is query inside another query and also perform the task related to join. Moreover
we learn the concept of left outer join, right outer join and full join and how these work in
program. All the task are completed in Visual Studio on North wind database.

Mujeeb Ijaz 01-131162-022

Vous aimerez peut-être aussi