Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

saad saeed 076 dblab manual

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

NAME: Saad Saeed

ROLL NO: 076


SUBJECT: DB LAB

WEEK 2, 3
CREATE DATABASE Lab2;

use Lab2;

CREATE TABLE Student (


RegistrationNumber VARCHAR(20) PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(100),
Session INT,
CGPA DECIMAL(3, 2),
Address VARCHAR(255)
);

INSERT INTO Student (RegistrationNumber, Name, Department, Session, CGPA, Address)


VALUES ('1', 'Rehan Irfan', 'Computer Science', 2023, 3.75, '1234 Street'),
('2', 'Junaid Babar', 'Computer Science', 2023, 4.75, '1234 Street');

UPDATE Student
SET Name = 'Rehan 2 ', CGPA = 3.85
WHERE RegistrationNumber = '1';
SELECT * FROM Student;

SELECT * FROM Student


WHERE RegistrationNumber = '2';

DELETE FROM Student


WHERE RegistrationNumber = '1';

WEEK 4, 5
CREATE DATABASE Lab2_Home;

USE Lab2_Home;

CREATE TABLE Student (


RegistrationNumber VARCHAR(20) PRIMARY KEY,
Name VARCHAR(100),
Department VARCHAR(100),
Session INT,
Address VARCHAR(255)
);

CREATE TABLE Course (


Name VARCHAR(100),
Code VARCHAR(100) PRIMARY KEY
);
CREATE TABLE Enrollments (
StudentRegNo VARCHAR(20),
CourseName VARCHAR(100),
FOREIGN KEY (StudentRegNo) REFERENCES Student(RegistrationNumber),
FOREIGN KEY (CourseName) REFERENCES Course(Code)
);

CREATE TABLE Attendance (


StudentRegNo VARCHAR(20),
CourseName VARCHAR(100),
TimeStamp DATETIME,
Status int,
FOREIGN KEY (StudentRegNo) REFERENCES Student(RegistrationNumber),
FOREIGN KEY (CourseName) REFERENCES Course(Code)
);

INSERT INTO Student (RegistrationNumber, Name, Department, Session, Address)


VALUES
('1', 'Ahmed Khan', 'Computer Science', 2023, '1234 Green Street, Lahore'),
('2', 'Ayesha Malik', 'Electrical Engineering', 2023, '5678 Blue Avenue, Islamabad'),
('3', 'Bilal Ahmed', 'Mechanical Engineering', 2023, '9101 Red Road, Karachi');

UPDATE Student
SET Department = 'Software Engineering', Address = '4321 Yellow Street, Lahore'
WHERE RegistrationNumber = '1';

SELECT * FROM Student;


SELECT * FROM Student
WHERE RegistrationNumber = '2';

DELETE FROM Student


WHERE RegistrationNumber = '3';

INSERT INTO Course (Name, Code)


VALUES ('Database Systems', 'CS202');

UPDATE Course
SET Name = 'Advanced Database Systems'
WHERE Code = 'CS202';

SELECT * FROM Course;

SELECT * FROM Course


WHERE Code = 'CS202';

DELETE FROM Course


WHERE Code = 'CS202';

WEEK 6, 7

use Northwind;

SELECT
COUNT(OrderID) AS TotalOrders,
AVG(Freight) AS AverageFreight,
SUM(Freight) AS TotalFreight,
MAX(Freight) AS MaxFreight,
MIN(Freight) AS MinFreight
FROM Orders;

SELECT
ShipCountry,
COUNT(OrderID) AS TotalOrders,
AVG(Freight) AS AverageFreight
FROM Orders
GROUP BY ShipCountry
HAVING COUNT(OrderID) > 5;

SELECT
FirstName AS EmployeeFirstName,
LastName AS EmployeeLastName,
HireDate AS EmployeeHireDate
FROM Employees;

SELECT
E.FirstName,
E.LastName,
O.OrderDate
FROM Employees AS E
JOIN Orders AS O ON E.EmployeeID = O.EmployeeID;
SELECT ProductName
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);

SELECT ShippedDate, COUNT(OrderID) AS OrdersShipped


FROM Orders
GROUP BY ShippedDate
ORDER BY ShippedDate;

SELECT Supplier.Country
FROM Suppliers AS Supplier
GROUP BY Supplier.Country
HAVING COUNT(Supplier.SupplierID) >= 2;

SELECT MONTH(OrderDate) AS MonthNumber, COUNT(OrderID) AS OrdersDelayed


FROM Orders
WHERE ShippedDate > RequiredDate
GROUP BY MONTH(OrderDate)
ORDER BY MonthNumber;

SELECT OrderID, SUM(Discount) AS TotalDiscount


FROM [Order Details]
WHERE Discount > 0
GROUP BY OrderID;
SELECT ShipCity, COUNT(OrderID) AS OrdersShipped
FROM Orders
WHERE ShipCountry = 'USA' AND YEAR(ShippedDate) = 1997
GROUP BY ShipCity;

SELECT ShipCountry, COUNT(OrderID) AS OrdersDelayed


FROM Orders
WHERE ShippedDate > RequiredDate
GROUP BY ShipCountry;

SELECT OrderID, SUM(Discount) AS TotalDiscount, SUM(UnitPrice * Quantity * (1 - Discount))


AS TotalPrice
FROM [Order Details]
GROUP BY OrderID
HAVING SUM(Discount) > 0;

SELECT ShipRegion, ShipCity, COUNT(OrderID) AS OrdersShipped


FROM Orders
WHERE YEAR(ShippedDate) = 1997
GROUP BY ShipRegion, ShipCity
ORDER BY ShipRegion, ShipCity;
use practice;

CREATE TABLE Student (


RegNo VARCHAR(20) PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
GPA FLOAT,
Contact INT
);

INSERT INTO Student (RegNo, FirstName, LastName, GPA, Contact)


VALUES
('S001', 'Ahmed', 'Khan', 3.8, 123124),
('S002', 'Fatima', 'Ali', NULL, 12441),
('S003', 'Bilal', 'Ahmed', 3.2, 14234),
('S004', 'Sarah', 'Malik', NULL, 214234),
('S005', 'Imran', 'Shah', 4.0, 124214);

SELECT * FROM Student;

SELECT FirstName, GPA FROM Student;

SELECT * FROM Student


WHERE GPA > 3.5;
SELECT * FROM Student
WHERE GPA <= 3.5;

SELECT FirstName + ' ' + LastName AS FullName FROM Student;

use Northwind;

-- Example using DISTINCT


SELECT DISTINCT ShipCountry FROM Orders;

-- Example using WHERE clause


SELECT OrderID, ShipCountry FROM Orders WHERE ShipCountry = 'USA';

-- Example using ORDER BY clause


SELECT * FROM Orders ORDER BY OrderDate DESC;

-- wrong
SELECT * FROM Orders
WHERE ShipCountry = 'USA' AND OrderDate > '1997-01-01' OR ShipCountry = 'Canada';

-- correct
SELECT * FROM Orders
WHERE (ShipCountry = 'USA' AND OrderDate > '1997-01-01') OR ShipCountry = 'Canada';

-- null
SELECT GPA * 2 AS GPA_Doubled FROM Student;

--12. Use the DISTINCT operator to eliminate the duplicates in your SQL statement:
SELECT DISTINCT ShipCountry FROM Orders;

--13. Write at least 3 SQL statements using ORDER BY clause:


-- Order by `OrderDate` ascending
SELECT * FROM Orders ORDER BY OrderDate;

-- Order by `ShipCity` descending


SELECT * FROM Orders ORDER BY ShipCity DESC;

-- Order by `CustomerID` ascending


SELECT * FROM Orders ORDER BY CustomerID;

-- Home Task
--1. Write a query to report orders that were delayed shipped:

SELECT * FROM Orders


WHERE ShippedDate > OrderDate;

--2. Our employees belong to how many countries? List the names.
SELECT DISTINCT Country FROM Employees;

--3. Is there any employee who is not accountable?

SELECT * FROM Employees


WHERE ReportsTo IS NULL;

--4. List the names of products which have been discontinued.


SELECT ProductName FROM Products
WHERE Discontinued = 1;

--5. List the IDs of the orders on which discount was not provided.
SELECT OrderID FROM [Order Details]
WHERE [Order Details].Discount = 0;

--6. Enlist the names of customers who have not specified their region.
SELECT ContactName FROM Customers
WHERE Region IS NULL;

--7. Enlist the names of customers along with contact number who either belong to UK or
USA.
SELECT ContactName, Phone FROM Customers
WHERE Country IN ('UK', 'USA');

--9. In which countries, products were sold in year 1997?


SELECT DISTINCT ShipCountry FROM Orders
WHERE YEAR(OrderDate) = 1997;

--10. List the IDs of customers whose orders were never shipped.
SELECT CustomerID FROM Orders
WHERE ShippedDate IS NULL;

--11. Write a query to report suppliers with their ID, company name, and city.
SELECT SupplierID, CompanyName, City FROM Suppliers;

--12. Our employees belong to how many countries? List them who are used to live in
London.
SELECT COUNT(DISTINCT Country) FROM Employees;
SELECT * FROM Employees
WHERE City = 'London';

--13. List the names of products which have not been discontinued
SELECT ProductName FROM Products
WHERE Discontinued = 0;

---14. List the IDs of the orders on which discount was 0.1 or less.
SELECT OrderID FROM [Order Details]
WHERE Discount <= 0.1;
--15. Enlist the IDs, names of employees and their contact number with extensions who have
not specified their region.

SELECT EmployeeID, LastName, FirstName, Extension FROM Employees


WHERE Region IS NULL;

WEEK 8, 9,10

Query:
SELECT Customers.CustomerID, Customers.ContactName, Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Reason: A LEFT JOIN is used to include all customers and any orders they
might have. Customers who have not placed any orders will still appear in the
result set, with NULL values for order-related columns.

Query:
SELECT Customers.CustomerID, Customers.ContactName, Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderID IS NULL;

Reason: This query uses a LEFT JOIN to find customers who have not placed
any orders. The WHERE Orders.OrderID IS NULL condition filters out
customers who do not have corresponding entries in the Orders table.

Query:
SELECT Customers.CustomerID, COUNT(OrderID) AS TotalOrders

FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
GROUP BY Customers.CustomerID;

Reason: The LEFT JOIN here ensures that all customers are included in the
count, even those who have not placed any orders. For customers without
orders, the count will be 0. And then group them by id so we have one row for
each customer

Query:
SELECT Customers.CustomerID, COUNT(Orders.OrderID) AS TotalOrders, SUM([Order
Details].Quantity) AS TotalQuantity
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
LEFT JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
WHERE Customers.Country = &#39;USA&#39;
GROUP BY Customers.CustomerID;
Reason: This LEFT JOIN approach ensures that even customers who do not have any orders
are included in the results, with their TotalOrders and TotalQuantity as 0 if they have no
orders.

Double LEFT JOIN: The first LEFT JOIN includes all customers even if they have no
orders. The second LEFT JOIN ensures that order details are included for customers with
orders. This allows for calculating total orders and quantities while still including customers
who have not placed any orders.

Query:
SELECT Customers.CustomerID, Customers.CompanyName, Orders.OrderID,
Orders.OrderDate
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE Orders.OrderDate = &#39;1997-04-07&#39;;

Reason: The LEFT JOIN includes all customers in the results, but the WHERE clause filters
to only show orders placed on a specific date. Customers without orders on this date will
appear with NULL values for order-related columns.

Query:
SELECT Customers.CustomerID, Orders.OrderID, Orders.OrderDate
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID
WHERE OrderDate BETWEEN &#39;1997-07-01&#39; AND &#39;1997-07-31&#39;;

Reason: An INNER JOIN is used to find only those customers who have orders within the
specified date range. This join ensures that only customers with matching entries in the
Orders table are included.

Query:
SELECT Products.ProductName, Orders.OrderDate
FROM Products
INNER JOIN [Order Details] ON [Order Details].ProductID = Products.ProductID
INNER JOIN Orders ON Orders.OrderID = [Order Details].OrderID
WHERE OrderDate = &#39;1997-08-08&#39;;

Reason: This query uses INNER JOIN to link products to their orders and then filter the
results to show only those orders placed on a specific date. Only products associated with
orders on that date will appear.

Query:
SELECT o.ShipAddress AS Address, o.ShipCity AS City, o.ShipCountry AS Country
FROM Orders o
INNER JOIN Employees e ON o.EmployeeID = e.EmployeeID
WHERE e.FirstName = &#39;Anne&#39; AND o.ShippedDate &gt; o.RequiredDate;

Reason: An INNER JOIN is used to link orders to employees, specifically focusing on orders
handled by a particular employee. The query then filters to find orders that were shipped
late.

Query:
SELECT DISTINCT Orders.ShipCountry AS Country
FROM Orders
INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID
INNER JOIN Products ON [Order Details].ProductID = Products.ProductID
INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID
WHERE Categories.CategoryName = &#39;Beverages&#39;;

Reason: INNER JOIN is used here to connect orders with products and their categories. The
query then filters to get the unique list of countries where

WEEK 11, 12

create database lastdb;

use lastdb;

CREATE TABLE [dbo].[Employee_Details]


(
[Emp_Id] INT IDENTITY(1,1) NOT NULL,
[Emp_Name] NVARCHAR(50) NOT NULL,
[Emp_City] NVARCHAR(50) NOT NULL,
[Emp_Salary] INT NOT NULL,
CONSTRAINT [PK_Employee_Details] PRIMARY KEY CLUSTERED ([Emp_Id] ASC)
);

INSERT INTO Employee_Details (Emp_Name, Emp_City, Emp_Salary) VALUES


('Pankaj', 'Alwar', 25000),
('Rahul', 'Jaipur', 26000),
('Rajan', 'Delhi', 27000),
('Sandeep', 'Alwar', 28000),
('Sanjeev', 'Jaipur', 32000),
('Narendra', 'Alwar', 34000),
('Neeraj', 'Delhi', 29000),
('Div', 'Jaipur', 25000),
('Tanuj', 'Alwar', 22000),
('Nitin', 'Jaipur', 20000);

CREATE TABLE [dbo].[Employee_Contact]


(
[Emp_Id] INT NOT NULL,
[MobileNo] NVARCHAR(15) NOT NULL,
CONSTRAINT [FK_Employee_Contact] FOREIGN KEY ([Emp_Id]) REFERENCES
Employee_Details([Emp_Id])
);

INSERT INTO Employee_Contact (Emp_Id, MobileNo) VALUES


(1, '9813220191'),
(2, '9813220192'),
(3, '9813220193'),
(4, '9813220194'),
(5, '9813220195'),
(6, '9813220196'),
(7, '9813220197'),
(8, '9813220198'),
(9, '9813220199'),
(10, '9813220135');
CREATE VIEW Employee_View1 AS
SELECT * FROM Employee_Details;

CREATE VIEW Employee_View2 AS


SELECT Emp_Id, Emp_Name, Emp_City FROM Employee_Details;

CREATE VIEW Employee_View3 AS


SELECT * FROM Employee_Details WHERE Emp_Id > 3;

CREATE VIEW Employee_View4 AS


SELECT Employee_Details.Emp_Id, Employee_Details.Emp_Name,
Employee_Details.Emp_Salary, Employee_Contact.MobileNo
FROM Employee_Details
LEFT OUTER JOIN Employee_Contact
ON Employee_Details.Emp_Id = Employee_Contact.Emp_Id
WHERE Employee_Details.Emp_Id > 2;

SELECT * FROM Employee_View1;

DROP VIEW Employee_View1;

EXEC sp_rename 'Employee_View4', 'Employee_ViewNew';


EXEC sp_helptext 'Employee_View4';

ALTER VIEW Employee_View4 AS


SELECT Employee_Details.Emp_Id, Employee_Details.Emp_Name,
Employee_Details.Emp_Salary, Employee_Contact.MobileNo
FROM Employee_Details
LEFT OUTER JOIN Employee_Contact
ON Employee_Details.Emp_Id = Employee_Contact.Emp_Id
WHERE Employee_Details.Emp_Id > 5 AND Employee_Details.Emp_City = 'Alwar';

EXEC sp_refreshview 'Employee_View1';

CREATE VIEW Employee_Details3 WITH SCHEMABINDING AS


SELECT Emp_Id, Emp_Name, Emp_Salary, Emp_City FROM dbo.Employee_Details;

CREATE VIEW Employee_Details4 WITH ENCRYPTION AS


SELECT Emp_Id, Emp_Name, Emp_Salary, Emp_City FROM dbo.Employee_Details;

CREATE VIEW Employee_Details7 AS


SELECT * FROM Employee_Details WHERE Emp_Salary > 30000 WITH CHECK OPTION;
INSERT INTO Employee_Details7 (Emp_Name, Emp_City, Emp_Salary)
VALUES ('Ram', 'Mumbai', 35000);

SELECT * FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE WHERE TABLE_NAME =


'Employee_Details';

SELECT * FROM sys.all_views;


SELECT * FROM sys.databases;

You might also like