You are on page 1of 32

DBMS

PRACTICAL
FILE

Name: PIYUSH KUMAR


Roll no: 21291761030
Course: MOR SOUTH CAMPUS
SEM: 2

Submitted to: Dr. Gurjeet Kaur & Dr. Jagvinder Singh


TABLE OF CONTENTS
SQL
Practical 1 DDL commands

Practical 2 DML command (INSERT,SELECT)


Practical 3 DML command (UPDATE,DELETE)
Practical 4 Aggregate Functions
Practical 5 GROUP-BY and ORDER-BY

Practical 6 JOINS
Practical 7 SQL sub-queries
VISUAL BASIC
Practical 8 Simple Calculator
Practical 9 EOQ Model

Practical 10 Simple Interest Calculator

Practical 11 Percentage and CGPA

Practical 12 Largest of 10 numbers ( using Array)


PRACTICAL 1
DDL COMMANDS
CREATE DATABASE Northwind_Piyush;
USE Northwind_Piyush;

CREATE TABLE customers (


customer_id INT NOT NULL PRIMARY KEY,
company_name VARCHAR(40) NOT NULL,
contact_name VARCHAR(30),
contact_title VARCHAR(30),
address VARCHAR(60),
city VARCHAR(15),
region VARCHAR(15),
postal_code VARCHAR(10),
country VARCHAR(15),
phone VARCHAR(24),
fax VARCHAR(24)
);

CREATE TABLE employees (


employee_id INT NOT NULL PRIMARY KEY,
last_name VARCHAR(20) NOT NULL,
first_name VARCHAR(10) NOT NULL,
title VARCHAR(30),
TitleOfCourtesy VARCHAR(25),
birth_date date,
hire_date date,
address VARCHAR(60),
city VARCHAR(15),
region VARCHAR(15),
postal_code VARCHAR(10),
country VARCHAR(15),
home_phone VARCHAR(24),
extension VARCHAR(4),
notes VARCHAR(50),
reports_to INT,
FOREIGN KEY (reports_to) REFERENCES employees(employee_id)
);

CREATE TABLE suppliers (


supplier_id INT NOT NULL PRIMARY KEY,
company_name VARCHAR(40) NOT NULL,
contact_name VARCHAR(30),
contact_title VARCHAR(30),
address VARCHAR(60),
city VARCHAR(15),
region VARCHAR(15),
postal_code VARCHAR(10),
country VARCHAR(15),
phone VARCHAR(24),
fax VARCHAR(24)
);

CREATE TABLE categories (


category_id INT NOT NULL PRIMARY KEY,
category_name VARCHAR(15) NOT NULL,
description VARCHAR(25)
);

-- Changing the size of varchar as description could be longer


ALTER TABLE categories
ALTER COLUMN description VARCHAR(250);

CREATE TABLE products (


product_id INT NOT NULL PRIMARY KEY,
product_name VARCHAR(40) NOT NULL,
supplier_id INT,
category_id INT,
quantity_per_unit VARCHAR(20),
unit_price INT,
units_in_stock INT,
units_on_order INT,
reorder_level INT,
discontinued INT NOT NULL,
FOREIGN KEY (category_id) REFERENCES categories(category_id),
FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id)
);

CREATE TABLE shippers (


shipper_id INT NOT NULL PRIMARY KEY,
company_name VARCHAR(40) NOT NULL,
phone VARCHAR(24)
);

CREATE TABLE orders (


order_id INT NOT NULL PRIMARY KEY,
customer_id INT,
employee_id INT,
order_date date,
required_date date,
shipped_date date,
ship_via INT,
freight INT,
ship_name VARCHAR(40),
ship_address VARCHAR(60),
ship_city VARCHAR(15),
ship_region VARCHAR(15),
ship_postal_code VARCHAR(10),
ship_country VARCHAR(15),
FOREIGN KEY (customer_id) REFERENCES customers(customer_id),
FOREIGN KEY (employee_id) REFERENCES employees(employee_id),
FOREIGN KEY (ship_via) REFERENCES shippers(shipper_id)
);

CREATE TABLE order_details (


order_id INT NOT NULL,
product_id INT NOT NULL,
unit_price INT NOT NULL,
quantity INT NOT NULL,
discount INT NOT NULL,
PRIMARY KEY (order_id, product_id),
FOREIGN KEY (product_id) REFERENCES products(product_id),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);

-- Deleting all the records (if in case database cleanup is required)


TRUNCATE TABLE orders;
TRUNCATE TABLE order_details;

-- Dropping ORDERS related records (in case order-related details are


no longer required)
DROP TABLE orders;
DROP TABLE order_details;
Object Explorer snippet after Command execution:

The following commands could also be used to get an overview of the table:
sp_help 'categories';
sp_help 'customers';
sp_help 'employees';
sp_help 'order_details';
sp_help 'orders';
sp_help 'products';
sp_help 'shippers';
sp_help 'suppliers';
PRACTICAL 2
DML Commands (INSERT & SELECT)
-- Account which are not of Assests types
SELECT TOP 15 percent *
FROM DimAccount
WHERE AccountType <> 'Assets';

-- Upper-Income category
SELECT Concat(FirstName,' ', MiddleName,' ', LastName) as Name,
Gender, MaritalStatus
FROM DimCustomer
WHERE YearlyIncome >= 100000;
-- All the currencies present in the dataset DimCurrency
SELECT *
FROM DimCurrency;

-- Adding more currencies which are not present in the dataset


INSERT INTO DimCurrency VALUES('RON', 'Romanian leu');
INSERT INTO DimCurrency VALUES('ETB', ' Ethiopian birr');
INSERT INTO DimCurrency VALUES('FKP', ' Falkland Islands pound');
INSERT INTO DimCurrency VALUES('FOK', ' Faroese króna');

SELECT * FROM DimCurrency;


-- Employees with less number of off-days for special bonuses

SELECT Concat(FirstName,' ', MiddleName,' ', LastName) as Name,


DepartmentName, BaseRate, VacationHours
FROM DimEmployee
WHERE Status ='Current' AND VacationHours <= 24; -- Still working and
took less than 24hrs off from work, to give bonuses etc.
PRACTICAL 3
DML Commands (UPDATE & DELETE)
-- Base rate increament by 15% for the hardworking employees
UPDATE DimEmployee
SET DimEmployee.BaseRate = DimEmployee.BaseRate*1.15
WHERE VacationHours <=24 and Status = 'Current';

SELECT Concat(FirstName,' ', MiddleName,' ', LastName) as Name,


DepartmentName, BaseRate, VacationHours
FROM DimEmployee
WHERE Status ='Current' AND VacationHours <= 24;
BEFORE:

AFTER:
-- In the employee data, there are 2 Barbara's and considering the
case one of them wants to modify their name
SELECT *
FROM DimEmployee
WHERE FirstName = 'Barbara';

UPDATE DimEmployee
SET FirstName = 'Barbra'
WHERE EmployeeKey = 182;

SELECT * FROM DimEmployee WHERE FirstName = 'Barbara';


AFTER UPDATE QUERY EXECUTION, THERE IS ONLY 1 Barbara in the dataset:

-- Deleting Barbara's records (in case she's no longer an employee)


DELETE FROM DimEmployee WHERE FirstName = 'Barbara';
SELECT * FROM DimEmployee WHERE FirstName = 'Barbara';
PRACTICAL 4
AGGREGATE FUNCTIONS
--2. Average yearly income of Customers
SELECT AVG(YearlyIncome) as Average_Yearly_Income
FROM DimCustomer;

-- No of Male/Female Employees
SELECT COUNT(CASE WHEN DimEmployee.Gender='M' THEN 1 END) As
Male,COUNT(CASE WHEN Gender='F' THEN 1 END) As Female,COUNT(CASE WHEN
(Gender='' OR Gender IS Null) THEN 1 END) As 'NotAssigned', COUNT(*)
as Total
FROM DimEmployee;

-- Employees having least Base Rate


SELECT FirstName,DepartmentName,BaseRate AS Minimum_BaseRate
FROM DimEmployee
WHERE BaseRate= (SELECT MIN(BaseRate) FROM DimEmployee);
-- Employees having highest yearly income

SELECT Concat(FirstName,' ', MiddleName,' ', LastName) as Name,


Gender, MaritalStatus,YearlyIncome as HighestIncome
FROM DimCustomer
WHERE YearlyIncome = (SELECT MAX(YearlyIncome) FROM DimCustomer);

-- Total price if 1 of each product type is bought on Listed Price


SELECT SUM(ListPrice) as Total_Price
FROM DimProduct;
PRACTICAL 5
GROUP by and ORDER by
-- Working employees categorization in different departments

SELECT DepartmentName, COUNT(CASE WHEN DimEmployee.Gender='M' THEN 1


END) As Male,COUNT(CASE WHEN Gender='F' THEN 1 END) As
Female,COUNT(CASE WHEN (Gender='' OR Gender IS Null) THEN 1 END) As
'NotAssigned', COUNT(*) as Total
FROM DimEmployee
WHERE Status = 'Current'
GROUP BY DepartmentName;
-- Working employees categorization in different departments ordered
by total employees

SELECT DepartmentName, COUNT(CASE WHEN DimEmployee.Gender='M' THEN 1


END) As Male,COUNT(CASE WHEN Gender='F' THEN 1 END) As
Female,COUNT(CASE WHEN (Gender='' OR Gender IS Null) THEN 1 END) As
'NotAssigned', COUNT(*) as Total
FROM DimEmployee
WHERE Status = 'Current'
GROUP BY DepartmentName
ORDER BY Total DESC;
PRACTICAL 6
JOIN Commands
-- Customers with their full addresses

SELECT Concat(DimCustomer.FirstName,' ', DimCustomer.MiddleName,'


',DimCustomer.LastName) as Name, DimCustomer.Gender,
DimCustomer.YearlyIncome, concat(DimGeography.City,',
',DimGeography.StateProvinceName,' ,
',DimGeography.EnglishCountryRegionName,' - ',DimGeography.PostalCode)
as Address
FROM DimCustomer
INNER JOIN DimGeography ON DimCustomer.GeographyKey
=DimGeography.GeographyKey;
-- Filteration of customers by region

SELECT Concat(DimCustomer.FirstName,' ', DimCustomer.MiddleName,' ',


DimCustomer.LastName) as Name, DimCustomer.Gender,
DimCustomer.YearlyIncome, concat(DimGeography.City,',
',DimGeography.StateProvinceName,' ,
',DimGeography.EnglishCountryRegionName,' - ',DimGeography.PostalCode)
as Address
FROM DimCustomer
INNER JOIN DimGeography ON DimCustomer.GeographyKey
=DimGeography.GeographyKey
WHERE DimGeography.StateProvinceName = 'California';

-- Reseller details on Customers with their addresses

SELECT Concat(DimCustomer.FirstName,' ', DimCustomer.MiddleName,' ',


DimCustomer.LastName) as Name, DimCustomer.Gender,
DimCustomer.YearlyIncome, concat(DimGeography.City,',
',DimGeography.StateProvinceName,' ,
',DimGeography.EnglishCountryRegionName,' - ',DimGeography.PostalCode)
as Address, DimReseller.ResellerName, DimReseller.Phone
FROM DimCustomer
INNER JOIN DimGeography ON DimCustomer.GeographyKey
=DimGeography.GeographyKey
INNER JOIN DimReseller ON DimGeography.GeographyKey =
DimReseller.GeographyKey;

-- Table for Customer categorization in different city with respect to


Gender
SELECT DimGeography.City, COUNT(CASE WHEN DimCustomer.Gender='M' THEN
1 END) As Male,COUNT(CASE WHEN Gender='F' THEN 1 END) As
Female,COUNT(CASE WHEN (Gender='' OR Gender IS Null) THEN 1 END) As
'NotAssigned', COUNT(*) as Total
FROM DimCustomer
INNER JOIN DimGeography ON DimCustomer.GeographyKey
=DimGeography.GeographyKey
GROUP BY DimGeography.City;
-- Table for customer categorization in different countries with
respect to Gender

SELECT DimGeography.EnglishCountryRegionName, COUNT(CASE WHEN


DimCustomer.Gender='M' THEN 1 END) As Male,COUNT(CASE WHEN Gender='F'
THEN 1 END) As Female,COUNT(CASE WHEN (Gender='' OR Gender IS Null)
THEN 1 END) As 'NotAssigned', COUNT(*) as Total
FROM DimCustomer
INNER JOIN DimGeography ON DimCustomer.GeographyKey
=DimGeography.GeographyKey
GROUP BY DimGeography.EnglishCountryRegionName;
PRACTICAL 7
SQL Sub-Queries

-- Employee names working in Accounts dept. and ordered by bdate


SELECT name as Accounts_Employees, Bdate
FROM Employees
WHERE Dno = (SELECT Dno FROM Departments WHERE Dname='Accounts')
ORDER BY Bdate;

-- Employee names having salary more than or equal to average salary


of Dept.3
SELECT name, Salary
FROM Employees
WHERE Salary >= (SELECT AVG(Salary) FROM Employees WHERE Dno = 3);
-- Employee name having third highest salary
SELECT name, Salary
from Employees
WHERE Salary=(SELECT MIN(t.Salary) AS Third_Highest_Salary FROM
(SELECT TOP 3 * FROM Employees ORDER BY Salary DESC) as t);

-- Employee SSN and names from Employee relation if Dno exists in


Department relation and department is ‘Accounts’
SELECT SSN, name
FROM Employees
WHERE EXISTS (SELECT Dno FROM Departments WHERE Departments.Dno =
Employees.Dno AND Dname='Accounts');

-- Employee name having salary greater than Venkat


SELECT name, Salary
FROM Employees
WHERE Salary > (SELECT Salary FROM Employees WHERE name = 'Venkat');
PRACTICAL 8
SIMPLE CALCULATOR
Dim a As Double
If Sign = " + " Then
Dim b As Double
Dim res As Double res = Val(a) + Val(b)
Dim Sign As String Text1.Text = res
Private Sub Label1_Click() Else
Text1.Text = Text1.Text & 1 If Sign = " - " Then
End Sub res = Val(a) - Val(b)
Text1.Text = res
Private Sub Command1_Click() Else
Text1.Text = Text1.Text & 1
End Sub
If Sign = " * " Then
res = Val(a) * Val(b)
Private Sub Command10_Click() Text1.Text = res
Text1.Text = Text1.Text & 0 Else
End Sub If Sign = " / " Then
res = Val(a) / Val(b)
Private Sub Command11_Click() Text1.Text = res
Text1.Text = Text1.Text & " . "
End If
End Sub
End If
Private Sub Command12_Click() End If
Text1.Text = " " End If
End Sub End Sub

Private Sub Command13_Click() Private Sub Command2_Click()


a = Val(Text1.Text)
Text1.Text = Text1.Text & 2
Sign = " + "
Text1.Text = " " End Sub
End Sub
Private Sub Command3_Click()
Private Sub Command14_Click() Text1.Text = Text1.Text & 3
a = Val(Text1.Text) End Sub
Sign = " - "
Text1.Text = " " Private Sub Command4_Click()
End Sub
Text1.Text = Text1.Text & 4
Private Sub Command15_Click() End Sub
a = Val(Text1.Text)
Sign = " * " Private Sub Command5_Click()
Text1.Text = " " Text1.Text = Text1.Text & 5
End Sub End Sub
Private Sub Command16_Click()
Private Sub Command6_Click()
a = Val(Text1.Text)
Sign = " / " Text1.Text = Text1.Text & 6
Text1.Text = " " End Sub
End Sub
Private Sub Command7_Click()
Private Sub Command17_Click() Text1.Text = Text1.Text & 7
b = Val(Text1.Text) End Sub

Private Sub Command8_Click()


Text1.Text = Text1.Text & 8
FORM LAYOUT OF CALCULATOR

OUTPUT of Operation -> 5.5 * 7.865


PRACTICAL 9
EOQ CALCULATOR
Private Sub Command1_Click() Private Sub Command2_Click()
Dim lmb As Double txtlmb.Text = ""
Dim A As Double txta.Text = ""
Dim Ic As Double txtic.Text = ""
Dim Q As Double txtq.Text = ""
lmb = Val(txtlmb.Text) End Sub
A = Val(txta.Text)
Ic = Val(txtic.Text)
Q = Sqr((2 * A * lmb) / Ic)
txtq.Text = Q
End Sub

FORM LAYOUT:

OUTPUT:
PRACTICAL 10
SIMPLE INTEREST CALCULATOR
Private Sub Command1_Click()
Dim principle As Double
Dim rate As Double
Dim time As Integer
principle = Val(txtprinciple.Text)
rate = Val(txtrate.Text)
time = Val(txttime.Text)
txtSI = (principle * rate * time) / 100
End Sub
Private Sub Command2_Click()
txtprinciple.Text = ""
txtrate.Text = ""
txttime.Text = ""
txtSI.Text = ""
End Sub

FORM LAYOUT:
OUTPUT
PRACTICAL 11
PERCENTAGE & CGPA CALCULATOR
Private Sub Command1_Click() Private Sub Command2_Click()
Dim db As Double txtdb.Text = ""
Dim ec As Double txtec.Text = ""
Dim sch As Double txtsch.Text = ""
Dim mkt As Double txtmkt.Text = ""
Dim cv As Double txtcv.Text = ""
Dim total As Double txtpercent.Text = ""
Dim percent As Double txtcgpa.Text = ""
Dim cgpa As Double End Sub
db = Val(txtdb.Text)
ec = Val(txtec.Text)
sch = Val(txtsch.Text)
mkt = Val(txtmkt.Text)
cv = Val(txtcv.Text)
total = db + ec + sch + mkt + cv
percent = (total / 500) * 100
txtpercent.Text = percent
cgpa = percent / 9.5
If cgpa >= 10 Then
cgpa = 10
End If
txtcgpa.Text = cgpa
End Sub
OUTPUT:
PRACTICAL 12
Largest number out of 10 entered
numbers using Arrays
Private Sub SortArray(x() As Double)
Dim n As Double
Dim Temp As Double
Dim bln As Boolean
bln = True
Do While bln
bln = False
For n = LBound(x) To (UBound(x) - 1)
If x(n) > x(n + 1) Then
Temp = x(n)
x(n) = x(n + 1)
x(n + 1) = Temp
bln = True
End If
Next
Loop
End Sub

Private Sub cmdlarge_Click()


Dim num(9) As Double
Dim n As Double
Dim largest As Double

For i = 0 To 9 Step 1
begin:
n = Val(InputBox("Enter the number " & i + 1 & " : "))
num(i) = n
Next i
SortArray num
MsgBox ("Largest Number is " & num(9))
End Sub
…. And soo on to enter all the 10 number.
All the 10 numbers are: 65, 166, 21, 567, 777, 912, 55, 87, 12, 8

Final Output:

You might also like