0% found this document useful (0 votes)
36 views

XII SQL Practical Notes 2024-25

Practical notes

Uploaded by

asiff.ansari0612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

XII SQL Practical Notes 2024-25

Practical notes

Uploaded by

asiff.ansari0612
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

INFORMATICS PRACTICES

CLASS XII
PRACTICAL NOTES

QUERYING AND SQL FUCTION

1. Create the following table named "Charity" and write SQL queries for the tasks
that follow:
Table : Charity
P_ID LastName FirstName Address City Contribution
1 Bindra Jaspeet 5B, Gomti Nagar Lucknow 3500.50
2 Rana Monica 21 A, Bandra Mumbai 2768.00
3 Singh Jatinder 8, Punjabi Bagh Delhi 2000.50
4 Arora Satinder K/1,Shere Mumbai 1900.00
Punjab Colony
5 Krishnan Vineeta A-75,Adarsh
Nagar
i) Display all first names in lowercase.
ii) Display all last names of people of Mumbai city in uppercase.
iii) Display Person Id along with First 3 characters of his/her name.
iv) Display first name concatenated with last name for all the employees.
v) Display length of address along with Person ID.
vi) Display last 2 characters of City and Person ID.
vii) Display Last Names and First names of people who have "at" in the
second or third position in their first names.
viii) Display the position of ‘a’ in Last name in every row.
ix) Display Last Name and First name of people who have "a" as the last
character in their First names.
x) Display the first name and last name concatenated after removing the
leading and trailing blanks.
xi) Display Person Id, last names and contribution rounded to the nearest
rupee of all the persons.
xii) Display Person Id, last name and contribution with decimal digits
truncated of all the persons.
xiii) Display Last name, contribution and a third column which has
contribution divided by 10. Round it to two decimal points.
Ans. SQL command to creating table “Charity”-
CREATE TABLE CHARITY
(
P_ID int Primary Key,
LastName varchar(30),
FirstName varchar(30),
Address varchar(50),
City varchar(20),
Contribution decimal(10,2)
);
XII IP Practical Notes 2024-25

SQL command to Inserting data to the table “Charity”-

INSERT INTO charity VALUES


(1,‟Bindra‟,‟Jaspeet‟,‟5B,Gomti Nagar‟,‟Lucknow‟,3500.50);

INSERT INTO charity VALUES


(2,‟Rana‟,‟Monica‟,‟21 A, Bandra‟,‟Mumbai‟,2768.00);

INSERT INTO charity VALUES


(3,‟Singh‟,‟Jatinder‟,‟8, Punjabi Bagh‟,‟Delhi‟,2000.50);

INSERT INTO charity VALUES


(4,‟Arora‟,‟Satinder‟,‟K/1, Shere Punjab Colony‟,‟Mumbai‟,1900.00);

INSERT INTO charity VALUES


(5,‟Krishnan‟,‟Vineeta‟,‟A-75, Adarsh Nagar‟,null,null);

SQL Command to Display all record from table Charity


SELECT * FROM Charity;
i) Display all first names in lowercase.
SELECT Lower(FirstName) FROM Charity;
ii) Display all last names of people of Mumbai city in uppercase.
SELECT Upper(LastName) FROM Charity WHERE City like “Mumbai”;
iii) Display Person ID along with First 3 characters of his/her name.
SELECT P_ID, LEFT(FirstName,3) FROM Charity;
iv) Display first name concatenated with last name for all the employees.
SELECT Concat(FirstName,” “,LastName) FROM Charity;
v) Display length of address along with Person ID.
SELECT P_ID, Length(Address) FROM Charity;
vi) Display last 2 characters of City and Person ID.
SELECT P_ID, Right(City,2) FROM Charity;
vii) Display Last Names and First names of people who have "at" in the second or
third position in their first names.
SELECT FirstName, LastName FROM Charity
WHERE INSTR(FirstName,‟at‟) IN (2,3);
viii) Display the position of ‘a’ in Last name in every row.
SELECT INSTR(LastName,‟a‟) FROM Charity ;
ix) Display Last Name and First name of people who have "a" as the last character
in their First names.
SELECT LastName,FirstName FROM Charity
WHERE FirstName LIKE “%a”;
x) Display the first name and last name concatenated after removing the leading
and trailing blanks.
SELECT Concat(TRIM(FirstName),TRIM(LastName)) FROM Charity;
PREPARED BY TAPOSH KARMAKARPGT P a g e 2 | 23
AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

xi) Display Person Id, last names and contribution rounded to the nearest rupee of
all the persons.
SELECT P_ID, LastName, ROUND(Contribution,-3) FROM Charity;
xii) Display Person Id, last name and contribution with decimal digits truncated of
all the persons.
SELECT P_ID, LastName, TRUNCATE(Contribution,0) FROM Charity;
xiii) Display Last name, contribution and a third column which hascontribution
divided by 10. Round it to two decimal points.
SELECT P_ID, LastName, Contribution,
ROUND((Contribution/10),2) FROM Charity;

2. Consider the table "Grocery" and write SQL queries for the tasks that follow:
Table: Grocery

i)Display Item name, unit price along with Date of purchase for all the
Items.
ii) Display Item name along with Month (in number) when it was purchased
for all the items.
iii) Display Item name along with year in which it was purchased for all the
items.
iv) Display Item Id, Date of Purchase and day name of week (e.g. Monday) on
which it was purchased for all the items.
v) Display names of all the items that were purchased on Mondays or
Tuesdays.
vi) Display the day name of the week on which Rice was purchased.
vii) Display the Item name and unit price truncated to integer value (no
decimal digits)of all the items.
viii) Display current date
Ans. SQL command to creating table “Grocery”-

CREATE TABLE Grocery


(
Item_ID integer PRIMARY KEY,
ItemName varchar(20),
UnitPrice decimal(10,2),
Quantity integer,
Date_Purchase date
);

PREPARED BY TAPOSH KARMAKARPGT P a g e 3 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

SQL command to Inserting data to the table “Grocery”-

INSERT INTO Grocery VALUES(1,‟Rice‟,52.50,80,‟2010-02-01‟);


INSERT INTO Grocery VALUES(2,‟Wheat‟,25.40,50,‟2010-03-09‟);
INSERT INTO Grocery VALUES(3,‟Corn‟,50.80,100,‟2010-03-11‟);
INSERT INTO Grocery VALUES
(4,‟Semolina‟,28.90,50,‟2010-01-15‟);

i) Display Item name, unit price along with Date of purchase for all the Items.
SELECT ItemName, UnitPrice, Date_Purchase FROM Grocery;
ii) Display Item name along with Month (in number) when it was purchased for all
the items.
SELECT ItemName, Month(Date_Purchase) FROM Grocery;
iii) Display Item name along with year in which it was purchased for all the items.
SELECT ItemName, YEAR(Date_Purchase) FROM Grocery;
iv) Display Item Id, Date of Purchase and day name of week (e.g. Monday) on which
it was purchased for all the items.
SELECT ItemName, Date_Purchase, Dayname(Date_Purchase) FROM Grocery;
v) Display names of all the items that were purchased on Mondays or Tuesdays.
SELECT * FROM Grocery
WHERE Dayname(Date_Purchase) IN („Monday‟,‟Tuesday‟);
vi) Display the day name of the week on which Rice was purchased.
SELECT ItemName, dayname(Date_Purchase) FROM Grocery
WHERE ItemName like „Rice‟;

vii) Display the Item name and unit price truncated to integer value (no decimal
digits) of all the items.
SELECT ItemName, Truncate(UnitPrice) FROM Grocery;

3. Consider the following table :

PREPARED BY TAPOSH KARMAKARPGT P a g e 4 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

# MAX() – MAX() function is used to find the Highest Value of any column or any
expression based on a column. MAX() takes one argument which can be any
column name or a valid expression involving a column name.
a) To find the highest cost of any type of shoe in the factory.
SELECT MAX(cost)FROM shoes;

b) To find the highest cost of any shoe of type 'School'.


SELECT MAX(cost)FROM shoesWHERE type=„School‟;

PREPARED BY TAPOSH KARMAKARPGT P a g e 5 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

# MIN() – MIN() function is used to find the Lowest Value of any column or an
expression based on a column. MIN() takes one argument which can be any
column name or a valid expression involving a column name.
c) To find the lowest cost of any type of shoe in the factory.
SELECT MIN(cost)FROM shoes;

d) To find the lowest cost of any shoe of type 'School'.


SELECT MIN(cost)FROM shoes WHERE type=„School‟;

# AVG() – AVG() function is used to find the average value of any column or an
expression based on a column. AVG() takes one argument which can be any
column name or a valid expression involving a column name.
Limitation:The argument of AVG() function can be of numeric (int /
decimal) type only.Averages of String and Date type data are not defined.
e) To find the average margin from shoes table.
SELECT AVG(margin)FROM shoes;

f) To find the average quantity in stock for the shoes of type Sports.
SELECT AVG(qty)FROM shoes WHERE type=„sports‟;

g) To find the average quantity in stock for the shoes of type Sports.
SELECT AVG(qty) FROM shoes WHERE type = 'Sports';

PREPARED BY TAPOSH KARMAKARPGT P a g e 6 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

# SUM() – SUM() function is used to find the Total Value of any column or an
expression based on a column. SUM() also takes one argument which can be any
column name or a valid expression involving a column name.
Like AVG(), the argument of SUM() function can be of numeric (int/decimal)
type only. Sums of String and Date type data are not defined.
h) To find the total quantity present in the stock.
SELECT SUM(qty)FROM shoes;

i) To find the total value (Quantity X Cost) of Shoes of type 'Office' present in the
inventory.
SELECT SUM(cost*qty)FROM shoes WHERE type = 'Office';

# COUNT() – COUNT() function is used to count the number of values in a column.


COUNT() takes one argument which can be any column name, an expression
based on a column, or an asterisk (*). If the argument is a *, then COUNT()
counts the total number of rows satisfying the condition, if any, in the table.
j) To count the total number of records in the table Shoes.
SELECT COUNT(*)FROM shoes;

k) To count the different types of shoes that the factory produces.


SELECT COUNT(distinct type) FROM shoes;

l) To count the number of customers in 'A' category.


SELECT COUNT(*) FROM customers WHERE category ='A';

PREPARED BY TAPOSH KARMAKARPGT P a g e 7 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

m) To count the number of orders of quantity more than 300.


SELECT COUNT(*)FROM orders WHERE order_qty > 300;

n) The management of the shoe factory may want to know what is the total quantity
of shoes of various types. i.e., what is the total quantity of shoes of type School,
Office, and Sports each.
SELECT type, SUM(qty) FROM shoes GROUP BY type;

o) The management may also want to know what is the maximum, minimum, and
average margin of each type of shoes.
SELECT type, MIN(margin), MAX(margin), AVG(margin)
FROM shoes GROUP BY type;

p) Find the total number of customers in each category.


SELECT category, COUNT(*) FROM customers GROUP BY category;

PREPARED BY TAPOSH KARMAKARPGT P a g e 8 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

q) The management of the shoe factory may want to know which shoes total
quantityis more than 1500.
SELECT type, SUM(qty) FROM shoes
GROUP BY type HAVING SUM(qty) > 1500;

Note :In these statements if we try to put the condition using WHERE instead of
HAVING, we shall get an error.
r) The management of the shoe factory may want to know what is the totalquantity of
shoes, of sizes other than 6, of various types. i.e., what is the totalquantity of shoes
(of sizes other than 6) of type School, Office, and Sports each. Moreover, the report
is required only for those groups for which the totalquantity is more than 1500.
SELECT type, SUM(qty) FROM shoes
WHERE size <>6 ->Checks individual row
GROUP BY type HAVING sum (qty) > 1500; ->Checks individual group

s) The management may also want to know what is the maximum, minimum,
andaverage margin of each type of shoes. But in this reports shoes of sizes 6 and
7only should be included. Report is required only for those groups for which
theminimum margin is more than 2.
SELECT type, MIN(margin), MAX(margin), AVG(margin) FROM shoes
WHERE size in (6,7)
GROUP BY type having MIN(margin) > 2;

t) The management of the shoe factory wants a report of orders which lists three
columns: Order_No, corresponding customer name, and phone number
SELECT order_no , name, phone
FROM orders, customers
WHERE orders.cust_code = customers.cust_code;
Or,
SELECT order_no , name, phone
FROM orders X, customers Y
WHERE X.cust_code = Y.cust_code;

PREPARED BY TAPOSH KARMAKARPGT P a g e 9 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

Output :

u) The management wants a four-column report containing order_no, order_qty,name


of the corresponding shoe and its cost.
SELECT order_no , Order_Qty, name, cost
FROM orders, shoes WHERE Shoe_Code = code;
Output :

v) The management wants the names of customers who have placed any order of
quantity more than 300.
SELECT name, address FROM orders, customers
WHERE orders.cust_code = customers.cust_code
and order_qty > 300;
Output :

w) The management wants a report in which with each Order_No management needs
name of the corresponding customer and also the total cost (Order quantity X Cost
of the shoe) of the order are shown.
SELECT order_no, Order_Qty, customers.name,
cost*order_qty AS 'Order Cost'
PREPARED BY TAPOSH KARMAKARPGT P a g e 10 | 23
AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

FROM orders, shoes, Customers


WHERE Shoe_Code = code
AND Orders.Cust_Code = Customers.Cust_Code ORDER BY order_no;
Or,
SELECT X.order_no, X.Order_Qty, Z.name,
Y.cost * X.order_qty AS 'Order Cost'
FROM orders X, shoes Y, Customers Z
WHERE X.Shoe_Code = Y.code
AND X.Cust_Code = Y.Cust_Code ORDER BY X.order_no;
Output :

4. In a database create the following tables with suitable constraints :


Table : STUDENTS

PREPARED BY TAPOSH KARMAKARPGT P a g e 11 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

Table : SPORTS

a) Based on these tables write SQL statements for the following queries:
i. Display the lowest and the highest classes from the table STUDENTS.
Ans. SELECT MIN(CLASS), MAX(CLASS) FROM STUDENTS
ii. Display the number of students in each class from the table STUDENTS.
Ans. SELECT CLASS, COUNT(CLASS) FROM STUDENTS
GROUP BY CLASS;
Output:
+-------+--------------+
| CLASS | COUNT(CLASS) |
+-------+--------------+
| 10 | 4 |
| 11 | 3 |
| 12 | 3 |
+-------+--------------+
iii. Display the number of students in class 10.
Ans. SELECT CLASS, COUNT(CLASS) FROM STUDENTS
GROUP BY CLASS HAVING CLASS=10;
Output:
+-------+--------------+
| CLASS | COUNT(CLASS) |
+-------+--------------+
| 10 | 4 |
+-------+--------------+
iv. Display details of the students of Cricket team.
Ans. SELECT X.*, Y.GAME FROM STUDENTS X, SPORTS Y
WHERE X.AdmNo=Y.AdmNo AND Y.Game LIKE „Cricket‟;
Output:
+-------+---------------+-------+-----+-----+------------------------+------------------+---------+
| AdmNo | Name | Class | Sec | RNo | Address | Phone | GAME |
+-------+---------------+-------+-----+-----+------------------------+------------------+---------+
| 1324 | Naresh Sharma | 10 | A | 1 | 31, Mohan Nagar | 435654 | Cricket |
| 1461 | David DSouza | 11 | B | 1 | D-34, Model Town | 243554, 98787665 | Cricket |
| 1434 | Varuna | 12 | B | 21 | 69, Rohini | | Cricket |
| 2328 | Peter Jones | 10 | A | 18 | 21/32B, Vishal Enclave | 24356154 | Cricket |
+-------+---------------+-------+-----+-----+------------------------+------------------+---------+

v. Display the Admission number, name, class, section, and roll number of
thestudents whose grade in Sports table is 'A'.

PREPARED BY TAPOSH KARMAKARPGT P a g e 12 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

Ans. SELECT X.AdmNo, Name, Class, Sec, RNo FROM STUDENTS X,


SPORTS Y WHERE X.AdmNo=Y.AdmNo AND Y.Grade=‟A‟;
Output:
+-------+----------------+-------+-----+-----+
| AdmNo | Name | Class | Sec | RNo |
+-------+----------------+-------+-----+-----+
| 1324 | Naresh Sharma | 10 | A | 1 |
| 1364 | Subya Akhtar | 11 | B | 13 |
| 2328 | Peter Jones | 10 | A | 18 |
| 2371 | Mohini Mehta | 11 | C | 12 |
| 1271 | Utkarsh Madaan | 12 | C | 1 |
| 1434 | Varuna | 12 | B | 21 |
+-------+----------------+-------+-----+-----+
vi. Display the name and phone numbers of the students of class 12 who are
playsome game.
Ans. SELECT X.name, Phone,Y.Game FROM STUDENTS X, SPORTS Y
WHERE X.AdmNo=Y.AdmNo AND X.Class=12;
Output:
+----------------+---------+-------------+
| name | Phone | Game |
+----------------+---------+-------------+
| Utkarsh Madaan | 4356154 | Volleball |
| Varuna | | Basket Ball |
| Utkarsh Madaan | 4356154 | Basket Ball |
| Varuna | | Cricket |
+----------------+---------+-------------+
vii. Display the Number of students with each coach.
Ans. SELECT CoachName, Count(Game) FROM SPORTS
GROUP BY CoachName;
Output:
+-------------+-------------+
| CoachName | Count(Game) |
+-------------+-------------+
| I. Malhotra | 5 |
| M.P. Singh | 2 |
| Narendra | 4 |
+-------------+-------------+
viii. Display the names and phone numbers of the students whose grade is 'A'
and whose coach is Narendra.
Ans. SELECT X.name, Phone FROM STUDENTS X, SPORTS Y
WHERE X.AdmNo=Y.AdmNo AND Y.Grade=‟A‟
AND Y.CoachName=‟Narendra‟;
Output:
+---------------+--------+
| name | Phone |
+---------------+--------+
| Naresh Sharma | 435654 |
| Varuna | |
+---------------+--------+
PREPARED BY TAPOSH KARMAKARPGT P a g e 13 | 23
AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

b) Predict the output of each of the following SQL statements, and then verify the
output by actually entering these statements:
i. SELECT class, sec, count(*) FROM students GROUP BY class,
sec;
Ans. +-------+-----+----------+
| class | sec | count(*) |
+-------+-----+----------+
| 10 | A | 3 |
| 10 | B | 1 |
| 11 | B | 2 |
| 11 | C | 1 |
| 12 | B | 1 |
| 12 | C | 2 |
+-------+-----+----------+
ii. SELECT Game, COUNT(*) FROM Sports GROUP BY Game;
Ans. +-------------+----------+
| Game | COUNT(*) |
+-------------+----------+
| Basket Ball | 5 |
| Cricket | 4 |
| Volleball | 2 |
+-------------+----------+
iii. SELECT game, name, address FROM students, Sports
WHERE students.admno = sports.admno AND grade = 'A';
Ans. +-------------+----------------+------------------------+
| game | name | address |
+-------------+----------------+------------------------+
| Cricket | Naresh Sharma | 31, Mohan Nagar |
| Volleball | Subya Akhtar | 12, Janak Puri |
| Basket Ball | Peter Jones | 21/32B, Vishal Enclave |
| Basket Ball | Mohini Mehta | 37, Raja Garden |
| Basket Ball | Utkarsh Madaan | C-32, Punjabi Bagh |
| Cricket | Varuna | 69, Rohini |
+-------------+----------------+------------------------+
iv. SELECT Game FROM students, Sports
WHERE students.admno = sports.admno
AND Students.AdmNo = 1434;
Ans. +-------------+
| Game |
+-------------+
| Basket Ball |
| Cricket |
+-------------+

PREPARED BY TAPOSH KARMAKARPGT P a g e 14 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

CARSHOWROOM DATABASE
Create a database called CARSHOWROOM has the following four relations:
 INVENTORY: Stores name, price, model, year of manufacturing, and fuel
type for each car in inventory of the showroom,
 CUSTOMER: Stores customer Id, name, address, phone number and
email for each customer,
 SALE: Stores the invoice number, car Id, customer id, sale date, mode of
payment, sales person‟s employee Id, and selling price of the car sold,
 EMPLOYEE: Stores employee Id, name, date of birth, date of joining,
designation, and salary of each employee in the showroom.
Table :INVENTORY
Field Type Null Default Comments
CarID varchar(4) No
CarName varchar(15) No
Price float No
Model varchar(10) No
YearManufacture int(11) No
Fueltype varchar(10) No

CarID CarName Price Model YearManufacture Fueltype


D001 Car1 582613 LXI 2017 Petrol
D002 Car1 673112 VXI 2018 Petrol
B001 Car1 567031 Sigma1.2 2019 Petrol
B002 Car2 647858 Delta1.2 2018 Petrol
E001 Car3 355205 5 STR STD 2017 CNG
E002 Car3 654914 CARE 2018 CNG
S001 Car4 514000 LXI 2017 Petrol
S002 Car4 614000 VXI 2018 Petrol

Table :CUSTOMER
Field Type Null Default Comments
CustId varchar(5) No
CustName varchar(20) No
CustAdd varchar(30) No
Phone varchar(10) No
Email varchar(20) No

PREPARED BY TAPOSH KARMAKARPGT P a g e 15 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

CustId CustName CustAdd Phone Email


C0001 AmitSaha L-10, Pitampura 4564587852 [email protected]
C0002 Rehnuma J-12, SAKET 5527688761 [email protected]
C0003 CharviNayyar 10/9, FF, Rohini 6811635425 [email protected]
C0004 Gurpreet A-10/2, SF, MayurVihar 3511056125 [email protected]

Table :SALE
Field Type Null Default Comments
InvoiceNo varchar(6) No
CarId varchar(5) No
CustId varchar(5) No
SaleDate date No
PaymentMode varchar(20) No
EmpID varchar(5) No
SalePrice float No

InvoiceNo CarId CustId SaleDate PaymentMode EmpID SalePrice


I00001 D001 C0001 2019-01-24 Credit Card E004 613247
I00002 S001 C0002 2018-12-12 Online E001 590321
I00003 S002 C0004 2019-01-25 Cheque E010 604000
I00004 D002 C0001 2018-10-15 Bank Finance E007 659982
I00005 E001 C0003 2018-12-20 Credit Card E002 369310
I00006 S002 C0002 2019-01-30 Bank Finance E007 620214

Table :EMPLOYEE
Field Type Null Default Comments
EmpID varchar(5) No
EmpName varchar(20) No
DOB date No
DOJ date No
Designation varchar(12) No
Salary int(11) No

EmpID EmpName DOB DOJ Designation Salary


E001 Rushil 1994-07-10 2017-12-12 Salesman 25550
E002 Sanjay 1990-03-12 2016-06-05 Salesman 33100
E003 Zohar 1975-08-30 1999-01-08 Peon 20000
E004 Arpit 1989-06-06 2010-12-02 Salesman 39100
PREPARED BY TAPOSH KARMAKARPGT P a g e 16 | 23
AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

E006 Sanjucta 1985-11-03 2012-07-01 Receptionist 27350


E007 Mayank 1993-04-03 2017-01-01 Salesman 27352
E010 Rajkumar 1987-02-26 2013-10-23 Salesman 31111

1. In order to increase sales, suppose the car dealer decides to offer his customers
to pay the total amount in 10 easy EMIs (equal monthly installments). Assume
that EMIs are required to be in multiples of 10,000. For that, the dealer wants
to list the CarID and Price along with the following data from the Inventory
table:
a) Calculate GST as 12% of Price and display the result after rounding it off to
one decimal place.
SELECT ROUND(12/100*Price,1) "GST" FROM INVENTORY;
+---------+
| GST |
+---------+
| 69913.6 |
| 80773.4 |
| 68043.7 |
| 77743.0 |
| 42624.6 |
| 78589.7 |
| 61680.0 |
| 73680.0 |
+---------+
b) Add a new column FinalPrice to the table inventory, which will have the
value as sum of Price and 12% of the GST.
ALTER TABLE INVENTORY ADD(FinalPrice Numeric(10,1));
UPDATE INVENTORY SET FinalPrice=Price+Round(Price*12/100,1);
SELECT * FROM INVENTORY;
+-------+---------+--------+-----------+-----------------+----------+------------+
| CarID | CarName | Price | Model | YearManufacture | Fueltype | FinalPrice |
+-------+---------+--------+-----------+-----------------+----------+------------+
| D001 | Car1 | 582613 | LXI | 2017 | Petrol | 652526.6 |
| D002 | Car1 | 673112 | VXI | 2018 | Petrol | 753885.4 |
| B001 | Car1 | 567031 | Sigma1.2 | 2019 | Petrol | 635074.7 |
| B002 | Car2 | 647858 | Delta1.2 | 2018 | Petrol | 725601.0 |
| E001 | Car3 | 355205 | 5 STR STD | 2017 | CNG | 397829.6 |
| E002 | Car3 | 654914 | CARE | 2018 | CNG | 733503.7 |
| S001 | Car4 | 514000 | LXI | 2017 | Petrol | 575680.0 |
| S002 | Car4 | 614000 | VXI | 2018 | Petrol | 687680.0 |
+-------+---------+--------+-----------+-----------------+----------+------------+
c) Calculate and display the amount to be paid each month (in multiples of
1000) which is to be calculated after dividing the FinalPrice of the car into 10
instalments. After dividing the amount into EMIs, find out the remaining
amount to be paid immediately, by performing modular division.
SELECT CarId, FinalPrice,
ROUND((FinalPrice- MOD(FinalPrice,10000))/10,0) "EMI",
MOD(FinalPrice,10000) "Remaining Amount" FROM INVENTORY;

PREPARED BY TAPOSH KARMAKARPGT P a g e 17 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

+-------+------------+-------+------------------+
| CarId | FinalPrice | EMI | Remaining Amount |
+-------+------------+-------+------------------+
| D001 | 652526.6 | 65000 | 2526.6 |
| D002 | 753885.4 | 75000 | 3885.4 |
| B001 | 635074.7 | 63000 | 5074.7 |
| B002 | 725601.0 | 72000 | 5601.0 |
| E001 | 397829.6 | 39000 | 7829.6 |
| E002 | 733503.7 | 73000 | 3503.7 |
| S001 | 575680.0 | 57000 | 5680.0 |
| S002 | 687680.0 | 68000 | 7680.0 |
+-------+------------+-------+------------------+

2. a) Add a new column Commission to the SALE table. The column Commission
should have a total length of 7 in which 2 decimal places to be there.
ALTER TABLE SALE ADD(Commission Numeric(7,2));
b) Calculate commission for sales agents as 12 per cent of the SalePrice, insert
the valuesto the newly added column Commission and then display records
of the table SALE where commission > 73000.
UPDATE SALE SET Commission=12/100*SalePrice;
SELECT * FROM SALE WHERE Commission > 73000;
+-----------+-------+--------+------------+--------------+-------+-----------+------------+
| InvoiceNo | CarId | CustId | SaleDate | PaymentMode | EmpID | SalePrice | Commission |
+-----------+-------+--------+------------+--------------+-------+-----------+------------+
| I00001 | D001 | C0001 | 2019-01-24 | Credit Card | E004 | 613247 | 73589.64 |
| I00004 | D002 | C0001 | 2018-10-15 | Bank Finance | E007 | 659982 | 79197.84 |
| I00006 | S002 | C0002 | 2019-01-30 | Bank Finance | E007 | 620214 | 74425.68 |
+-----------+-------+--------+------------+--------------+-------+-----------+------------+

c) Display InvoiceNo, SalePrice and Commission such that commission value is


rounded off to 0.
SELECT InvoiceNo, SalePrice, Round(Commission,0) FROM SALE;
+-----------+-----------+---------------------+
| InvoiceNo | SalePrice | Round(Commission,0) |
+-----------+-----------+---------------------+
| I00001 | 613247 | 73590 |
| I00002 | 590321 | 70839 |
| I00003 | 604000 | 72480 |
| I00004 | 659982 | 79198 |
| I00005 | 369310 | 44317 |
| I00006 | 620214 | 74426 |
+-----------+-----------+---------------------+

d) Display the details of SALE where payment mode is credit card.


SELECT * FROM SALE WHERE PAYMENTMODE='CREDIT CARD';
+-----------+-------+--------+------------+-------------+-------+-----------+------------+
| InvoiceNo | CarId | CustId | SaleDate | PaymentMode | EmpID | SalePrice | Commission |
+-----------+-------+--------+------------+-------------+-------+-----------+------------+
| I00001 | D001 | C0001 | 2019-01-24 | Credit Card | E004 | 613247 | 73589.64 |
| I00005 | E001 | C0003 | 2018-12-20 | Credit Card | E002 | 369310 | 44317.20 |
+-----------+-------+--------+------------+-------------+-------+-----------+------------+

3. a) Display customer name in lower case and customer email in upper case from

PREPARED BY TAPOSH KARMAKARPGT P a g e 18 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

table CUSTOMER.
SELECT LOWER(CustName), UPPER(Email) FROM CUSTOMER;
+-----------------+---------------------+
| LOWER(CustName) | UPPER(Email) |
+-----------------+---------------------+
| amitsaha | [email protected] |
| rehnuma | [email protected] |
| charvinayyar | [email protected] |
| gurpreet | [email protected] |
+-----------------+---------------------+
b) Display the length of the email and part of the email from the email ID
before the character „@‟. Note – Do not print „@‟.
SELECT LENGTH(Email), LEFT(Email, INSTR(Email, "@")-1)
FROM CUSTOMER;
+---------------+----------------------------------+
| LENGTH(Email) | LEFT(Email, INSTR(Email, "@")-1) |
+---------------+----------------------------------+
| 19 | amitsaha2 |
| 19 | rehnuma |
| 19 | charvi123 |
| 19 | gur_singh |
+---------------+----------------------------------+
c) Let us assume that four digit area code is reflected in the mobile number
starting from position number 3. For example, 1851 is the area code of
mobile number 9818511338. Now, write the SQL query to display the area
code of the customer living in Rohini.
SELECT MID(Phone,3,4) FROM CUSTOMER
WHERE CustAdd like “%Rohini%”;
+----------------+
| MID(Phone,3,4) |
+----------------+
| 1163 |
+----------------+

d) Display emails after removing the domain name extension “.com” from
emails of the customers.
SELECT TRIM(“.com” from Email) FROM CUSTOMER;
+-------------------------+
| TRIM(".com" from Email) |
+-------------------------+
| amitsaha2@gmail |
| rehnuma@hotmail |
| charvi123@yahoo |
| gur_singh@yahoo |
+-------------------------+

PREPARED BY TAPOSH KARMAKARPGT P a g e 19 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

e) Display details of all the customers having yahoo emails only.


SELECT * FROM CUSTOMER WHERE Email LIKE "%yahoo%";
+--------+--------------+------------------------+------------+---------------------+
| CustId | CustName | CustAdd | Phone | Email |
+--------+--------------+------------------------+------------+---------------------+
| C0003 | CharviNayyar | 10/9, FF, Rohini | 6811635425 | [email protected] |
| C0004 | Gurpreet | A-10/2, SF, MayurVihar | 3511056125 | [email protected] |
+--------+--------------+------------------------+------------+---------------------+

f) Using the table INVENTORY, Convert the CarMake to uppercase if its value
starts with the letter „B‟.

g) Using the table INVENTORY, If the length of the car‟s model is greater than
4 then fetch the substring starting from position 3 till the end fromattribute
Model.
SELECT MODEL, LENGTH(MODEL), SUBSTR(MODEL,3)
FROM INVENTORY WHERE LENGTH(MODEL)>4;
+-----------+---------------+-----------------+
| MODEL | LENGTH(MODEL) | SUBSTR(MODEL,3) |
+-----------+---------------+-----------------+
| Sigma1.2 | 8 | gma1.2 |
| Delta1.2 | 8 | lta1.2 |
| 5 STR STD | 9 | STR STD |
+-----------+---------------+-----------------+
h) Using the table EMPLOYEE, Display employee name and the last 2
characters of his EmpId.
SELECT CONCAT(EMPNAME,RIGHT(EMPID,2)) FROM EMPLOYEE;
+--------------------------------+
| CONCAT(EMPNAME,RIGHT(EMPID,2)) |
+--------------------------------+
| Rushil01 |
| Sanjay02 |
| Zohar03 |
| Arpit04 |
| Sanjucta06 |
| Mayank07 |
| Rajkumar10 |
+--------------------------------+
i) Using the table EMPLOYEE, Display designation of employee and the
position of character „e‟ in designation, if present.
SELECT DESIGNATION, INSTR(DESIGNATION,'E') FROM EMPLOYEE;

PREPARED BY TAPOSH KARMAKARPGT P a g e 20 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

+--------------+------------------------+
| DESIGNATION | INSTR(DESIGNATION,'E') |
+--------------+------------------------+
| Salesman | 4 |
| Salesman | 4 |
| Peon | 2 |
| Salesman | 4 |
| Receptionist | 2 |
| Salesman | 4 |
| Salesman | 4 |
+--------------+------------------------+

4. a) Using the table EMPLOYEE, Select the day, month number and year of
joining of all employees.
SELECT DAY(DOJ), MONTH(DOJ), YEAR(DOJ) FROM EMPLOYEE;
+----------+------------+-----------+
| DAY(DOJ) | MONTH(DOJ) | YEAR(DOJ) |
+----------+------------+-----------+
| 12 | 12 | 2017 |
| 5 | 6 | 2016 |
| 8 | 1 | 1999 |
| 2 | 12 | 2010 |
| 1 | 7 | 2012 |
| 1 | 1 | 2017 |
| 23 | 10 | 2013 |
+----------+------------+-----------+

b) If the date of joining is not a Sunday, then display it in the following format
"Wednesday, 26, November, 1979."
SELECT DAYNAME(DOJ), DAY(DOJ),MONTHNAME(DOJ), YEAR(DOJ)
FROM EMPLOYEE WHEREDAYNAME(DOJ)!='Sunday';
+--------------+----------+----------------+-----------+
| DAYNAME(DOJ) | DAY(DOJ) | MONTHNAME(DOJ) | YEAR(DOJ) |
+--------------+----------+----------------+-----------+
| Tuesday | 12 | December | 2017 |
| Friday | 8 | January | 1999 |
| Thursday | 2 | December | 2010 |
| Wednesday | 23 | October | 2013 |
+--------------+----------+----------------+-----------+

c) Display the day of birth for all employees whose salary is more than 25000.
SELECT EMPNAME, DAYNAME(DOB), SALARY FROM EMPLOYEE
WHERE SALARY>25000;

PREPARED BY TAPOSH KARMAKARPGT P a g e 21 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

+----------+--------------+--------+
| EMPNAME | DAYNAME(DOB) | SALARY |
+----------+--------------+--------+
| Rushil | Sunday | 25550 |
| Sanjay | Monday | 33100 |
| Arpit | Tuesday | 39100 |
| Sanjucta | Sunday | 27350 |
| Mayank | Saturday | 27352 |
| Rajkumar | Thursday | 31111 |
+----------+--------------+--------+

5. a) Display the total number of records from table INVENTORY having a model
as VXI.
SELECT COUNT(*) FROM INVENTORY WHERE Model=”VXI”;
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+

b) Display the total number of different types of Models available from table
INVENTORY.
SELECT COUNT(DISTINCT Model) FROM INVENTORY;
+-----------------------+
| COUNT(DISTINCT Model) |
+-----------------------+
| 6 |
+-----------------------+

c) Display the average price of all the cars with Model LXI from table
INVENTORY.
SELECT AVG(Price) FROM INVENTORY WHEREModel="LXI";
+------------+
| AVG(Price) |
+------------+
| 548306.5 |
+------------+

d) Find sum of Sale Price of the cars purchased by the customer having ID
C0001 from table SALE.
SELECT SUM(SALEPRICE) FROM SALE WHERE CUSTID=‟C0001‟;
OR,
SELECT SUM(SALEPRICE) FROM SALE GROUP BY CUSTID HAVING
CUSTID=‟C0001‟;
+----------------+
| SUM(SALEPRICE) |
+----------------+
| 1273229 |
+----------------+

PREPARED BY TAPOSH KARMAKARPGT P a g e 22 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313
XII IP Practical Notes 2024-25

6. a) Display the number of cars purchased by each customer from the SALE
table.
SELECT CustID, COUNT(*) "Number of Cars" FROM SALE
GROUP BY CustID;
+--------+----------------+
| CustID | Number of Cars |
+--------+----------------+
| C0001 | 2 |
| C0002 | 2 |
| C0003 | 1 |
| C0004 | 1 |
+--------+----------------+
b) Display the customer Id and number of cars purchased if the customer
purchased more than 1 car from SALE table.
SELECT CustID, COUNT(*) FROM SALE
GROUP BY CustID HAVING Count(*)>1;
+--------+----------+
| CustID | COUNT(*) |
+--------+----------+
| C0001 | 2 |
| C0002 | 2 |
+--------+----------+

c) Display the number of people in each category ofpayment mode from the
table SALE.
SELECT PaymentMode, COUNT(PaymentMode) FROM SALE
GROUP BY Paymentmode ORDER BY Paymentmode;
+--------------+--------------------+
| PaymentMode | COUNT(PaymentMode) |
+--------------+--------------------+
| Bank Finance | 2 |
| Cheque | 1 |
| Credit Card | 2 |
| Online | 1 |
+--------------+--------------------+
d) Display the PaymentMode and number of payments made using that mode
more than once.
SELECT PaymentMode, Count(PaymentMode) FROM SALE
GROUP BY Paymentmode HAVING COUNT(*)>1 ORDER BY Paymentmode;
+--------------+--------------------+
| PaymentMode | Count(PaymentMode) |
+--------------+--------------------+
| Bank Finance | 2 |
| Credit Card | 2 |
+--------------+--------------------+
7.

PREPARED BY TAPOSH KARMAKARPGT P a g e 23 | 23


AIR FORCE SCHOOL JORHAT |MOBILE : 7002070313

You might also like