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

sql : data visualization, data analysis, etc

Uploaded by

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

sql : data visualization, data analysis, etc

Uploaded by

lokeshshukla191
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Data Visualization with SQL

Q 24 to Q 26) Consider the following table HOSPITAL.


Table: HOSPITAL

PNo Name Age Department DateOfAdm Charges Sex


1 Mayank 65 Surgery 23/02/2018 600 M
2 Babita 24 ENT 01/01/2019 400 F
3 Kashish 45 Orthopedic 19/12/2018 400 M
4 Tarun 12 Surgery 01/10/2018 600 M
5 Manisha 36 ENT 12/01/2018 400 F
6 Imran 16 ENT 24/02/2018 400 M
7 Ankita NUL Cardiology 20/08/2018 800 F
L
8 Zoya 45 Gynecology 22/02/2018 500 F
9 Kush 19 Cardiology 13/01/2019 800 M
10 Shalini 31 Medicine 19/02/2018 300 F
Note: PNo is the primary key in the above table.

24. Write SQL commands for the statements (a) to (g) on the table HOSPITAL.
(a) To show all the information about the patients of the cardiology department.

(b) To list the names of female patients who are either in the orthopaedic or surgery
department.
(c) To list the name of all the patients with their date of admission in ascending order.

(d) To display the patient's name, charges, the age for female patients only.
(e) To count the number of patients with age > 30.
(f) To display various departments.
(g) To display the number of patients in each department.

Ans. (a) select * fIom hospital where Department=' Cardiology';


(b) select name from hospital where Sex='F and (Department='Cardiology' or Department
Surgery ');

OR
select name from hospital where Sex='F' and Department in ('Cardiology', 'Surgery');

(c) select name, DateofAdm from hospital order by DateofAdm;

(d) select name, Charges, Age from hospital where Sex = ‘F’;
(e) select count(Age) from hospital where Age > 30;

(f) select distinct(Department) from hospital;

(g) select department, count(department) from hospital group by department;


25. Write SQL commands for the statements (a) to (g) on the table HOSPITAL.

(a). To display the details of all the patients whose name starts with the alphabet Z'. (b)
To change the age of the patient Kush to 20.
(c) To increase the charges of all the patients by 5%.
(d) To remove the record of the patient whose Name is Tarun.

(e) To add another column DocName(Doctor Name) of the type varchar in the above
table.

(f) To display patient detail whose age is missing(null).


(g) To decrease the charges by 5% of all the patients admitted to the ENT department.

Ans. (a) select* from hospital where name like 'Z%’,

(b) update hospital set Age=20 where Name='Kush';

(c) update hospital set Charges = Charges + (Charges *5)/100;


(d) delete from hospital where Name = 'Tarun';
(e) Alter table hospital add DocName varchar (20);
(f) select* from hospital where Age is NULL;

(g) update hospital set Charges = Charges - (Charges*5) /100 where Department = ‘ENT’

26. Write SOL commands for the statements (a) to (h) on the table HOSPITAL

(a) To insert a new row in the HOSPITAL table with the following data: 11,' Kasif', 37,
ENT', 2018 02-25, 300, 'M’.
(b) To set charges to NULL for all the patients in the Surgery department.

(c) To display patient details who are giving charges in the range 300 and 400 (both
inclusive).

(d) To display the details of that patient whose name second character contains a'. (e) To
display total charges of ENT Department.
(f) To display details of the patients who admitted in the year 2019.
(g) To display the structure of the table hospital.

(h) Write the command to create the above table.


Ans. (a) Insert into hospital values ( (11, Kasif', 37, 'ENT', 'M);

(b) update hospital set Charges = NULL where Department='Surgery';

(c) select* from hospital where charges between 300 and 400;

(d) select* from hospital where name like ‘_a%';

(e) selectsum( (Charges) from hospital where Department =' ENT';

(f) select* from hospital where year(DateofAdm) =2019;

(g) desc hospital;


(h) Create table Hospital (
Pno int(3) Primary Key,
Name Varchar(20).

Age int(2),
Department Varchar(15),
DateofAdm date,
Charges int(4),

Sex char(1) );

27. Answers the following question. Use appropriate label for each column.
(a) Write a SQL statement to find the square of the price field in the 'PRODUCTS' table.

(b) Write a SQL statement to convert the name field of the 'CUSTOMERS' table to
uppercase.

(c) Write a SQL statement to extract the first 3 characters from the 'ITEM CODE' field of
the 'INVENTORY’ table.

(d) Write a SQL statement to calculate the sum of the 'QUANTITY' field in the 'SALES'
table.

(e) Write a SQL statement to find the average price of all the products in the
'PRODUCTS' table.
(f) Write a SQL statement to count the number of products in the 'PRODUCTS' table. (g)
Write a SQL statement to group the 'SALES' data by the 'PRODUCT ID' field and find
the maximum quantity sold for each product.

(h) Write a SQLstatement to sort the'CUSTOMERS' data in descending order by their


'LAST PURCHASE DATE' field.

(i) Write a SQL statement to find the products in the 'PRODUCTS' table whose price is
greater than 100.
(j) Write a SQL statement to find the current date and time using the NOW() function.

Ans. (a) SELECT price * price AS "Price Squared" FROM PRODUCTS;


(b) SELECT UPPER(name) AS "Name in Uppercase" FROM CUSTOMERS;
(c) SELECT LEFT(ITEM CODE, 3) AS "First 3 Characters" FROM INVENTORY;

(d) SELECT SUM(QUANTITY) AS "Total Quantity Sold" FROM SALES;


(e) SELECTAVG(price) AS "Average Price" FROM PRODUCTS;
(f) SELECT COUNT(*) AS "Number of Products" FROM PRODUCTS;

(g) SELECT PRODUCT ID, MAX(QUANTITY) AS "Maximunn Quantity Sold" FROM


SALES GROUPBY PRODUCT ID;
(h) SELECT * FROM CUSTOMERS ORDER BY LAST PURCHASE DATE DESC;
(i) SELECT * FROM PRODUCTS WHERE price > 100;

(j) SELECT NOWO AS "Current Date and Time":

Q31) Consider the foll. Table Bank


Table: Bank

AccNo Cust_name FD_Amount Months Int_Rate FD_Rate


1001 Arti Gupta 30000 36 6.00 2018-07-01
1002 Dilip Lal 50000 48 6.75 2018-03-22
1003 Navin Gupta 30000 36 NULL 2018-03-01
1004 D.P. Yadav 80000 60 8.25 2017-06-12
1005 Jyoti Sharma 20000 36 6.50 2017-01-31
1006 Rakesh Kumar 70000 60 8.25 2018-06-15
1007 K.D Singh 50000 48 NULL 2018-07-05
1008 Anjali Sharma 60000 48 6.75 2017-04-02
1009 Swati Garg 40000 42 6.50 2018-06-15
1010 Rupinder Kaur 25000 36 6.50 2018-09-27
28. Write SQL commands for the statements (a) to (g) on the table BANK
(a) To create the table Bank (Primary Key: AccNo)
(b) Display the structure of the table Bank.
(c) Display the details of all the bank.

(d) Display the AccNo, Cust _Name, and FD_Amount.


(e) Display the details of all the FD's having maturity time is less than 40 months.
(f) Display the AccNo and FD amount which started before 0104-2018.

(g) Display details of all FD whose rate of interest are NULL.


Ans. (a) Create table Bank (
AccNo Int(4) Primary Key,
Cust_Name Varchar(15),

FD_Amount int,
Months int(3),
Int_Rate decimal(5,2),
FD_Date date );

(b) Desc Bank;

(c) Select * from Bank;


(d) SelectAccNo, Cust_Name, FD_Amount from Bank;

(e) Select * from Bank where Months < 40


(f) Select Acc_No, FD_Amount from Bank where FD_Date < ‘2018-04-01’;

(g) Select * from Bank where int_rate IS NULL;

29. Write SQL commands for the statenments (a) to (g) on the table BANK

(a) Display details of all the FD whose rate of interest is NOT NULL.

(b) Display amounts of various FD from the table Bank. An FD Amount should appear
only once.

(c) Display the number of months of various loans from the table Bank. A month should
appear only once.
(d) Display the Customer Name and FD Amount for all the Bank which do not have a
number of months is 36.

(e) Display the Customer Name and FD Amount for which the FD amount is less than
500000 or int rate is more than 7.

(f) Display the details of all FD which started in the year 2018.
(g) Display the details of all FD whose FD Amount is in the range 40000 to 50000.

Ans. (a) Select * from Bank where Int_rate IS NOT NULL;

(b) Select distinct(FD_Amount) from Bank;


(c) Select distinct(Months) from Bank;

(d) Select cust_name, FD_Amount from Bank where Months < > 36

(e) Select cust_name, Fd_Amount from Bank where Fd_Amount < 500000 and Int_rate >
7.00;

(f) Select * from Bank where year(FD_Date ) = 2018;


(g) Select * from Bank where FD_Amount IN (40000, 50000);

30.) Write SQL commands for the statements (a) to (h) on the table Bank
(a) Display the details of all FD whose rate of interest is in the range 6% to 7%.

(b) Display the Customer Name and FD Amount for all the loans for which the number of
Months is 24, 36, or 48(using IN operator).

(c) Display the Account Number, Customer Name and FD Amount for all the FD for
which the Customer Name ends with "Sharma".
(d) Delete the records of ‘Rupinder Kaur".

(e) Add another column Maturity_Amt of type Integer in the Bank table.
(f) To find the average FD amount. Label the column as Average FD Amount".
(g) To find the total FD amount which started in the year 2018?

(h) Update Maturity Amount of all bank customers. a. Maturity Amount = (FD
Amount*Months* Int rate)/(12*100)
Ans. (a) Select * from bank where Int rate>=6.00 and FD Amount<=7.00;
(b) Select cust name,FD Amount from Bank where Months in(36, 42, 48);

(c) Select AccNo, cust_name, Fd_Amount from bank where cust_name like ‘%Sharma’;

(d) Delete from Bank where cust_name= ‘Rupinder Kaur’;


(e) Alter table bank add Maturity_Amt int;
(f) Select avg(FD_Amount) “Average FD Amount” from bank;
(g) Select sum(FD_Int) from bank where year(FD_Date)=2018;

(h) Update Bank set Maturity_amt = ((FD_Amount)*Months*Int_rate)/(12*100);

25. Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of tables
FURNITURE and ARRIVALS
Table: FURNITURE

NO ITEMNAME TYPE DATEOFSTOC PRICE DISCOUNT


K
1 White lotus Double Bed
2 Pink feather Baby cot 20/01/2002 7000 20
3 Dolphin Baby cot 19/02/2002 9500 20
4 Decent Office Table 01/01/2002 25000 30
5 Comfort zone Double Bed 12/01/2002 25000 25
6 Donald Baby cot 24/02/2002 6500 15
7 Royal Finish Office Table 20/02/2002 18000 30
8 Royal tiger Sofa 22/02/2002 31000 30
9 Econo sitting Sofa 13/12/2001 9500 25
10 Eating Dining Table 19/02/2002 11500 25
Paradise

Table: ARRIVALS

NO ITEMNAM TYPE DATEOFST PRICE DISCOUNT


E OCK
1 Wood Double Bed 23/03/2003 25000 25
Comfort
2 Old Fox Sofa 20/02/2003 17000 20
3 Micky Baby cot 21/02/2003 7500 15
(a) To show all information about the Baby cost from the FURNITURE table.
(b) To list the ITEMNAME which are period at more than 15000 from the FURNITURE
table.
(c) To list ITEMNAME and TYPE of those items, in which date of stock is before
22/01/2002 from the FURNITURE table in descending order of ITEMNAME.
(d) To display ITEMNAME and DATEOFSTOCK of those items, in which the discount
t percentage is more than 25 from FURNITURE table.
(e) To count the number of items, whose TYPE is “Sofa” from FURNITURE table.
(f) To insert a new row in the ARRIVALS table with the following data:
14, “Valvet touch”,, “Double bed”, {25/03/03}, 25000, 30

(g) Give the output of following SQL statement

Note: Outputs of the above mentioned queries should be based on original data given in
both the tables i.e.. without considering the insertion done in (f) part of this question.

(i) Select COUNT(distinct TYPE) from FURNITURE: -

(ii) Select MAX(DISCOUNT) from FURNITURE. ARRIVALS;

(iii) Select AVG(DISCOUNT) from FURNITURE where TYPE="Baby cot";

(iv) Select SUM(Price) from FURNITURE where DATEOFSTOCK<12/02/02;

Ans:

a) SELECT * FROM FURNITURE WHERE TYPE = 'Baby cot';


b) SELECT ITEMNAME FROM FURNITURE WHERE PRICE > 15000;

c) SELECT ITEMNAME, TYPE FROM FURNITURE WHERE DATEOFSTOCK <


'22/01/2002' ORDER BY ITEMNAME DESC;

d) SELECT ITEMNAME, DATEOFSTOCK FROM FURNITURE WHERE


DISCOUNT > 25;
e) SELECT COUNT(*) AS SofaCount FROM FURNITURE WHERE TYPE = 'Sofa';

f) INSERT INTO ARRIVALS (NO, ITEMNAME, TYPE, DATEOFSTOCK, PRICE,


DISCOUNT)
VALUES (14, 'Valvet touch', 'Double bed', '25/03/2003', 25000, 30);
g) i) SELECT COUNT(DISTINCT TYPE) FROM FURNITURE;

ii) SELECT MAX(DISCOUNT) FROM (SELECT DISCOUNT FROM FURNITURE UNION


SELECT DISCOUNT FROM ARRIVALS) AS Combined;

iii) SELECT AVG(DISCOUNT) FROM FURNITURE WHERE TYPE = 'Baby cot';

iv) SELECT SUM(PRICE) FROM FURNITURE WHERE DATEOFSTOCK < '12/02/2002';


26. Consider the foll, tables GAAMES and PLAYER.

Write SQL commands for the statements (a) to (d) and give outputs for SQL queries e1 to
e4

Relation : GAMES

GCode Game Name Number Prize Money Schedule Date


101 Carrom Brd. 2 5000 23-Jan-2004
102 Badminton 2 12000 12-Dec-2003
103 Table Tennis 4 8000 14-Feb-2004
105 Chess 2 9000 01-Jan-2004
108 Lawn Tennis 4 25000 19-Mar-2004

Relation: PLAYER

PCode Name Gcode


1 Nabi Ahmad 101
2 Ravi Sahai 108
3 Jatin 101
4 Nazneen 103

a) To display the name of all Games with their Gcodes


(b) To display details of those games which are having PrizeMoney more than 7000.
(c) To display the content of the GAMES table in ascending order of ScheduleDate.

(a) To display sum of PrizeMoney for each of the Number of participation groupings (as
shown in column Number)
(el) Select COUNT(DISTINCT Number) FROM GAMES;
(e2) Select MAX(ScheduleDate), MIN(ScheduleDate) FROM GAMES;
(e3) Select SUM(PrizeMoney) FROM GAMES;
(e4) Select DISTINCT Gcode FROM PLAYER;

Solution:
a) SELECT GCode, GameName FROM GAMES;

b) SELECT * FROM GAMES WHERE PrizeMoney > 7000;

c) SELECT * FROM GAMES ORDER BY ScheduleDate ASC;

d) SELECT Number, SUM(PrizeMoney) AS TotalPrizeMoney


FROM GAMES
GROUP BY Number;

e1) SELECT COUNT(DISTINCT Number) FROM GAMES;

e2) SELECT MAX(ScheduleDate) AS LatestDate, MIN(ScheduleDate) AS EarliestDate


FROM GAMES;

e3) SELECT SUM(PrizeMoney) AS TotalPrizeMoney FROM GAMES;

e4) SELECT DISTINCT GCode FROM PLAYER;


27. Consider the foll. Tables WORKER and PAYLEVEL and answer (a) and (b) parts of
this question:
Relation: WORKER

ECODE NAME DESIG PAYLEVE DOJ DOB


L
11 Radhey Supervisor P001 13-Sep- 23-Aug-
Shyam 2004 1981
12 ChanderNath Operator P003 22-Feb- 12-Jul-1987
2010
13 Fizza Operator P003 14-June- 14-Oct-1983
2009
15 Ameen Ahm Mechanic P002 21-Aug- 13-Mar-
2006 1984
18 Sanya Clerk P002 19-Dec- 09-June-
2005 1983

Relation: PAYLEVEL

PAYLEVEL PAY ALLOWANCE


P001 26000 12000
P002 22000 10000
P003 12000 6000
(a) Write SQL commands for the following statements.

(i)To display the details of all WORKERS in descending order of DOB.

(ii) To display NAME and DESIG of those WORKERS whose PLEVEL is either
P001 or P002.

(iii) To display the content of all the WORKERS table, whose DOB is in between
'19-JAN- 1984' and '18-JAN-1987'.

(iv) To add a new row with the following: 19, 'Daya kishore', 'Operator', 'P003', '19-
Jun-2008', '11-Jul-1984'
(b) Give the output of the following SQL queries:

(i) Select count(plevel), plevel from worker group by plevel;

(ii) Select max(dob), min (doj) from worker;

(iii) Select name, pay from worker w, paylevel p where w.plevel=p.plevel and
w.ecode<13;

(iv) Select plevel, pay allowance from paylevel where plevel=' p003';

Solution: a) i) SELECT * FROM WORKER ORDER BY DOB DESC;

(ii) SELECT NAME, DESIG FROM WORKER WHERE PAYLEVEL IN ('P001',


'P002');

(iii)SELECT * FROM WORKER WHERE DOB BETWEEN '19-JAN-1984' AND '18-


JAN-1987';
(iv) INSERT INTO WORKER (ECODE, NAME, DESIG, PAYLEVEL, DOJ, DOB)
VALUES (19, 'Daya kishore', 'Operator', 'P003', '19-Jun-2008', '11-Jul-1984');

b) i) SELECT COUNT(PAYLEVEL), PAYLEVEL FROM WORKER GROUP BY


PAYLEVEL;

ii) SELECT MAX(DOB), MIN(DOJ) FROM WORKER;

iii) SELECT NAME, PAY FROM WORKER W, PAYLEVEL P WHERE


W.PAYLEVEL = P.PAYLEVEL AND W.ECODE < 13;

iv) SELECT PAYLEVEL, PAY, ALLOWANCE FROM PAYLEVEL WHERE


PAYLEVEL = 'P003';
28. Consider the foll. Tables CARHUB and CUSTOMER and answer (a) and (b) parts of
this question:
Table : CARHUB

Vcode Vehicle Make Color Capacity Charges


Name
100 Innova Toyota WHITE 7 15
102 SX4 Suzuki BLUE 4 14
104 C Class Mercedes RED 4 35
105 A-Star Suzuki WHITE 3 14
108 Indigo Tata SILVER 3 12

Table: CUSTOMER

CCode CName VCode


1 Hemant Sahu 101
2 Raj Lal 108
3 Feroz Shah 105
4 Ketan Dhal 104

a) Write SQL commands for the following statements:


(i) To display the names of all white colored vehicles

(ii) To display name of vehicle, make and capacity of vehicles in ascending order of their sitting
capacity
(iii) To display the highest charges at which a vehicle can be hired from CARHUB.
(iv) To display the customer name and the corresponding name of the vehicle hired by them.

(b) Give the output of the following SQL queries:


(i) Select count(distinct make) from cabhub;
(ii) Select max(charges), min(charges) from carhub;
(iii) Select count(*), make from carhub;

(iv) Select vehiclename from carhub where capacity = 4;


SOLUTION: a) i) SELECT VehicleName FROM CARHUB WHERE Color = 'WHITE';
ii) SELECT VehicleName, Make, Capacity FROM CARHUB ORDER BY Capacity ASC;
iii) SELECT MAX(Charges) AS HighestCharges FROM CARHUB;
iv) SELECT CUSTOMER.CName, CARHUB.VehicleName

FROM CUSTOMER
JOIN CARHUB ON CUSTOMER.VCode = CARHUB.VCode;
b) i) SELECT COUNT(DISTINCT Make) FROM CARHUB;

ii) SELECT MAX(Charges) AS MaxCharges, MIN(Charges) AS MinCharges FROM


CARHUB;
iii) SELECT COUNT(*), Make FROM CARHUB GROUP BY Make;
iv) SELECT VehicleName FROM CARHUB WHERE Capacity = 4;

29) Write SQL queries for (a) to (f) and write the outputs for the SQL queries mentioned shown
in (gl) to (g4) parts on the basis of tables ITEMS and TRADERS:

Table : ITEMS

CODE INAME QTY PRICE COMPANY TCODE


1001 DIGITAL 120 11000 XENITA T01
PAD 12i
1006 LED 70 38000 SANTORA T02
SCREEN 40
1004 CAR GPS 50 21500 GEOKNOW T01
SYSTEM
1003 DIGITAL 160 8000 DIGICLICK T02
CAMERA
12X
1005 PEN DRIVE 600 1200 STOREHOM T03
32GB E

Table: TRADERS

TCode TName CITY


T01 ELECTRONIC SALES MUMBAI
T03 BUSY STORE CORP DELHI
T02 DISP HOUSE INC CHENNAI

a) To display the details of all the iterns in the ascending order of item names (ie. INAME).

(b) To display item name and price of all those items. whose price is in range of 10000 and
22000 (both values inclusive).
(c) To display the number of items, which are traded by each trader. The expected output of this
query should be:
T01 2
T02 2
T03 1

(d) To display the price, item name and quantity (t.e qty) of those items which have quantity
more than 150.

(e) To display the names of those traders, who are either from DELHI or from MUMBAI.

(f) To display the names of the companies and the names of the items in descending order of
company names.

(g1) Select max(price), min(price) from items; I


(g2) Select price qty amount from items where code-1004;
(g3) Select distinct tcode from items;
(g4) Select iname, tname from items i, traders t where i.tcode=t.tcode and qty<100;

SOLUTION:
a) a) SELECT * FROM ITEMS ORDER BY INAME ASC;
b) SELECT INAME, PRICE FROM ITEMS WHERE PRICE BETWEEN 10000 AND 22000;
c) SELECT TCODE, COUNT(*) AS ItemCount FROM ITEMS GROUP BY TCODE;

d) SELECT PRICE, INAME, QTY FROM ITEMS WHERE QTY > 150;
e) SELECT TNAME FROM TRADERS WHERE CITY IN ('DELHI', 'MUMBAI');
f) SELECT COMPANY, INAME FROM ITEMS ORDER BY COMPANY DESC;

g1) SELECT MAX(PRICE) AS MaxPrice, MIN(PRICE) AS MinPrice FROM ITEMS;


g2) SELECT PRICE, QTY, PRICE * QTY AS Amount FROM ITEMS WHERE CODE = 1004;
g3) SELECT DISTINCT TCODE FROM ITEMS;

g4) SELECT INAME, TNAME FROM ITEMS I, TRADERS T WHERE I.TCODE = T.TCODE
AND QTY < 100;

31. Create the foll. Table named “Charity” and write SQL queries for the tasks that follow:

Table:Charity
P_Id LastName FirstName Address City Contribution
1 Bindra Jaspreet 5B, Gomti Lucknow 3500.50
Nagar
2 Rana Monica 21 A, Bandra Mumbai 2768.00
3 Singh Jatinder 8, Punjabi Delhi 2000.50
Bagh
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.

ii) 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.
SOLUTION: i) SELECT LOWER(FirstName) AS FirstNameLowercase FROM Charity;
ii) SELECT UPPER(LastName) AS LastNameUppercase FROM Charity WHERE City =
'Mumbai';

iii) SELECT P_Id, LEFT(FirstName, 3) AS FirstThreeChars FROM Charity;

iv) SELECT CONCAT(FirstName, ' ', LastName) AS FullName FROM Charity;


v) SELECT P_Id, LENGTH(Address) AS AddressLength FROM Charity;

vi) SELECT P_Id, RIGHT(City, 2) AS LastTwoCharsCity FROM Charity;

vii) SELECT LastName, FirstName


FROM Charity

WHERE SUBSTRING(FirstName, 2, 2) = 'at' OR SUBSTRING(FirstName, 3, 2) = 'at';


viii) SELECT LastName, LOCATE('a', LastName) AS PositionOfA FROM Charity;

ix) SELECT LastName, FirstName FROM Charity WHERE RIGHT(FirstName, 1) = 'a';

x) SELECT CONCAT(TRIM(FirstName), ' ', TRIM(LastName)) AS FullNameTrimmed FROM


Charity;

xi) SELECT P_Id, LastName, ROUND(Contribution, 0) AS RoundedContribution FROM


Charity;
xii) SELECT P_Id, LastName, TRUNCATE(Contribution, 0) AS TruncatedContribution FROM
Charity;

xiii) SELECT LastName, Contribution, ROUND(Contribution / 10, 2) AS


ContributionDividedBy10 FROM Charity;

32) Consider the table “Grocer” and write SQL queries for the tasks that follow:

Table: Charity

Item_Id ItemName UnitPrice Quantity (kg) Date_Purchase


1 Rice 52.50 80 2010-02-01
2 Wheat 25.40 50 2010-03-09
3 Corn 50.80 100 2010-03-11
4 Semolina 28.90 50 2010-01-15
(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.

(vi) Display the Item name and unit price truncated to integer value (no decimal digits) of all the
items.
(viii) Display current date.
Consider the following table Schooldata:

Table : Schooldata

Admno Name Grade Club Marks Gender


20150001 Sargam 12 STEM 86 Male
Singh
20110212 Alok Kumar 10 SPACE 75 Male
20090234 Mohit Gaur 11 SPACE 84 Male
20130216 Ramil Malik 10 READER 91 Male
20190227 Tanvi Batra 11 STEM 70 Female
20120200 Nomita 12 STEM 64 Female
Ranjan
Write SQL queries for the following:

i) Display the avg. Marks secured by each Gender


ii) Display the minimum Marks secured by the students of Grade 10.
iii) Display the total number of students in each Club where number of students are more
than 1.
OR
Display the max and min marks secured by each gender.

Solution: i) SELECT ItemName, UnitPrice, Date_Purchase FROM Grocer;


(ii) SELECT ItemName, MONTH(Date_Purchase) AS PurchaseMonth FROM Grocer;

(iii)SELECT ItemName, YEAR(Date_Purchase) AS PurchaseYear FROM Grocer;

(iv) SELECT Item_Id, Date_Purchase, DAYNAME(Date_Purchase) AS DayOfWeek FROM


Grocer;

(v) SELECT ItemName FROM Grocer WHERE DAYNAME(Date_Purchase) IN ('Monday',


'Tuesday');

(vi) SELECT DAYNAME(Date_Purchase) AS DayOfWeek FROM Grocer WHERE


ItemName = 'Rice';
(vii) SELECT ItemName, TRUNCATE(UnitPrice, 0) AS TruncatedUnitPrice FROM
Grocer;

(viii) SELECT CURDATE() AS CurrentDate;

SQL Queries for Table "Schooldata"

i) SELECT Gender, AVG(Marks) AS AvgMarks FROM Schooldata GROUP BY Gender;

ii) SELECT MIN(Marks) AS MinMarks FROM Schooldata WHERE Grade = 10;

iii) SELECT Club, COUNT(*) AS StudentCount FROM Schooldata GROUP BY Club


HAVING COUNT(*) > 1;

iv) SELECT Gender, MAX(Marks) AS MaxMarks, MIN(Marks) AS MinMarks FROM


Schooldata GROUP BY Gender;

You might also like