Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

SQL Lab

Uploaded by

Mehak Yahya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

SQL Lab

Uploaded by

Mehak Yahya
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 09
(Fall 2023)

Course: Database Management System Lab

Course Code: CSL 220 Max Marks: 25

Faculty’s Name: Maryam Munawar Lab Engineer:

Name: MEHAK Enroll No: 03-134221-015

Lab Task1
Create the following table
CREATE TABLE writer (
poet varchar(50) default NULL,
anthology varchar(40) default NULL,
copies_in_stock int null
default NULL
);

Task 2
Insert the following values

INSERT INTO writer VALUES


('Mongane Wally Serote','Tstetlo',3),
('Mongane Wally Serote', 'No Baby Must Weep',8),
('Mongane Wally Serote','A Tough Tale',2),
('Douglas Livingstone', 'The Skull in the Mud',21),
('Douglas Livingstone','A Littoral Zone',2);
Task 3
Practice the following queries

1. SELECT * FROM writer;

2. SELECT poet,SUM(copies_in_stock) as stock FROM writer GROUP BY poet;

3. SELECT SUM(copies_in_stock) FROM writer GROUP BY poet;

4. SELECT poet,
MAX(copies_in_stock) max,
MIN(copies_in_stock) min,
AVG(copies_in_stock) avg,
SUM(copies_in_stock) sum
FROM writer GROUP BY poet;
5. SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer WHERE copies_in_stock > 5 GROUP BY poet;

6. SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer GROUP BY poet HAVING (copies_in_stock) > 5;

CORRECT QUERY:
SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer GROUP BY poet,(copies_in_stock) HAVING (copies_in_stock) > 5;

7. SELECT MAX(copies_in_stock)
FROM writer
GROUP BY poet
HAVING COUNT(copies_in_stock) > 1;

8. SELECT poet,
MAX(copies_in_stock) AS max,
MIN(copies_in_stock) AS min,
AVG(copies_in_stock) AS avg,
SUM(copies_in_stock) AS sum
FROM writer GROUP BY poet HAVING poet > 'E';

Task 4

SQL CREATE VIEW Syntax


CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name WHERE condition

SQL CREATE VIEW Examples

If you have the Northwind database you can see that it has several views installed
bydefault.

The view "Current Product List" lists all active products (products
that are not Discontinued) from the "Products" table.

The view is created with the following SQL:

CREATE VIEW [Current Product List] AS


SELECT ProductID,ProductName
FROM Products WHERE Discontinued=No

We can query the view above as follows:


SELECT * FROM [Current Product List]

Another view in the Northwind sample database selects every product


in the"Products" table with a unit price higher than the average unit price:

CREATE VIEW [Products Above Average Price] AS


SELECT ProductName,UnitPrice
FROM Products
WHERE UnitPrice>(SELECT AVG(UnitPrice) FROM Products)

We can query the view above as follows:


SELECT * FROM [Products Above Average Price]
Another view in the Northwind database calculates the total sale for each category
in1997. Note that this view selects its data from another view called "Product Sales
for1997":

CREATE VIEW [Category Sales For 1997] AS


SELECT DISTINCT CategoryName,Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName

We can query the view above as follows:


SELECT * FROM [Category Sales For 1997]

We can also add a condition to the query. Now we want to see the total sale only for the
category "Beverages":

SELECT * FROM [Category Sales For 1997]


WHERE CategoryName='Beverages'

Task 5
Write a short note on this lecture. What did you learn?

We learned about SQL views, which are virtual tables created from SELECT queries. The
examples using the Northwind database showed how views can simplify complex queries and
enhance data organization. Views are valuable tools for managing and analyzing data in
relational databases.

Lab Grading Sheet :

Max Obtained
Task Comments(if any)
Marks Marks
1. 5
2. 5
3. 5
4. 5
5. 5

Total 25 Signature

Note : Attempt all tasks and get them checked by your Lab. Instructor

You might also like