sql : data visualization, data analysis, etc
sql : data visualization, data analysis, etc
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.
OR
select name from hospital where Sex='F' and Department in ('Cardiology', 'Surgery');
(d) select name, Charges, Age from hospital where Sex = ‘F’;
(e) select count(Age) from hospital where Age > 30;
(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.
(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.
(c) select* from hospital where charges between 300 and 400;
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.
(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.
FD_Amount int,
Months int(3),
Int_Rate decimal(5,2),
FD_Date date );
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.
(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;
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’;
25. Write SQL commands for (a) to (f) and write the outputs for (g) on the basis of tables
FURNITURE and ARRIVALS
Table: FURNITURE
Table: ARRIVALS
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.
Ans:
Write SQL commands for the statements (a) to (d) and give outputs for SQL queries e1 to
e4
Relation : GAMES
Relation: PLAYER
(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;
Relation: PAYLEVEL
(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:
(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';
Table: CUSTOMER
(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.
FROM CUSTOMER
JOIN CARHUB ON CUSTOMER.VCode = CARHUB.VCode;
b) i) SELECT COUNT(DISTINCT Make) FROM CARHUB;
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
Table: TRADERS
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.
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;
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
(vii) Display Last Names and First names of people who have "at" in the second or third position
in their first names.
(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';
32) Consider the table “Grocer” and write SQL queries for the tasks that follow:
Table: Charity
(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