SQL Test 2_IP_answers
SQL Test 2_IP_answers
6. Display the Pattaid, Location and Landtype of lands whose marketvalue is from 2500000 to 3000000.
select Pattaid, Location, Landtype from landdetails where marketvalue between 2500000 and 3000000;
7. Display the PattaID, SurveyNumber of those Lands whose Area size is not from 3 to 5.
select Pattaid, SurveyNumber from landdetails where area not between 3 and 5;
10. Display the records of those lands whose registration date is from May 2018 to Feb 2020.
select * from landdetails where registrationdate between ‘2018-05-01’ and ‘2020-02-29’;
11. Display the records of lands from Chennai, Erode and Salem.
Select * from landdetails where location in (‘Chennai’, ‘Erode’, ‘Salem’);
12. Display the records of land which are not from Chennai, Erode and Salem.
Select * from landdetails where location not in (‘Chennai’, ‘Erode’, ‘Salem’);
Select * from landdetails where location != ‘Chennai’ or location != ‘Erode’ or location!= ‘Salem’;
13. Display the Ownername of those owners whose name begin with ‘R’
select ownername from landdetails where ownername like “R%”;
14. Display the Ownername of those owners whose name end with ‘I’
select ownername from landdetails where ownername like “%I”;
15. Display the Ownername of those owners whose name has the second letter as ‘a’.
select ownername from landdetails where ownername like “_a%”;
16. Display status wise maximum market price.
Select status, max(marketvalue) from landdetails group by status;
18. Display sum of market price for each type of land whose sum of market price is more than 5000000.
Select landtype, sum(marketvalue) from landdetails group by landtype having sum(marketvalue)
> 5000000;
20. Display those records which are of land type agriculture and whose status is not active.
Select * from landdetails where landtype = ‘Agriculture’ and status != ‘Active’
21. Display average of market value for each type of land whose average market value is more than 3500000 in
descending order of average of market value.
Select landtype, avg(marketvalue) from landdetails group by landtype having avg(marketvalue) >
3500000 order by avg(marketvalue) desc;
22. Display total cost for all the lands where total cost = market value * area.
Select marketvalue*area as “Total cost” from landdetails;
Machinery table:
(ii) Write command to remove the primary key from the table.
Alter table machinery drop primary key;
(iii) Write command to set serial_number as the primary key.
Alter table machinery add primary key(serial_number);
(iv) Write command to add a column named “Status” with data type as varchar(25).
Alter table machinery add status varchar(25);
(v) Write command to remove the column purchase_date from the table.
Alter table machinery drop purchase_date;
(vi) Write command to change the name of the column Type to “MachineType”.
Alter table machinery change Type MachineType varchar(30);
(vii) Write command to add constraint not null to the column purchase_date.
Alter table machinery change Purchase_date Purchase_date date not null;
(or)
Alter table machinery modify PURCHASE_date date not null;
(viii) Write command to remove the above table from the database.
Drop table machinery;
(xi) Write command to show all the tables under the selected database;
show tables;
Machinery table:
Stationery_Stock table
quantit
item_id item_name category supplier purchase_date cost_per_unit
y
201 Notebook Paper Goods 50 ABC Suppliers 2022-06-15 40.00
202 Pen Writing 100 XYZ Traders 2023-08-20 10.00
203 Pencil Writing 75 LMN Stationers 2024-02-05 5.00
204 Eraser Accessories 60 ABC Suppliers 2022-11-30 3.00
205 Marker Writing 40 XYZ Traders 2023-09-25 25.00
206 Stapler Office Tools 30 LMN Stationers 2024-01-10 120.00
207 Glue Stick Adhesives 45 ABC Suppliers 2023-07-18 35.00
1. Display the Item_name and month of purchase of products purchased in the year 2022.
select item_name, monthname(purchase_date) from stationary_stock where year(purchase_date)
= 2022;
2. Display the records that are purchased in the month of September.
Select * from stationary_stock where monthname(purchase_date) = “September”;
3. Display the records purchased on Tuesday
Select * from stationary_stock where dayname(purchase_date) = “Tuesday”;
4. Display year wise maximum cost per unit.
Select year(purchase_date), max(cost_per_unit) from Stationery_Stock group by year(Purchase);
5. Display month name wise sum of cost per unit with sum of cost more than 100.
Select monthname(purchase_date), sum(cost_per_unit) from Stationery_Stock group by
monthname(purchase_date) having sum(cost_per_unit) > 100;
6. Display the records that were purchased after 20 day of a month.
Select * from stationary_stock where day(purchase_date) > 20;
7. Display the first three character of all item_names.
Select left(item_name, 3) from stationary_stock
8. Display the records whose no of characters in item_name is more than 7.
select * from statitonary_stock where length(item_name) > 7;
9. Display 4 character starting from 2nd character from supplier column for all the records.
Select mid(supplier, 2, 4) from statitonary_stock ;
10. Display the position of character ‘I’ for each items category.
Select instr(category, ‘i’) from statitonary_stock ;
11. Display the Item_name in lower case
Select lower(item_name) from statitonary_stock ;
12. Display the last three character of Category in upper case
Select upper(right(Category, 3)) from statitonary_stock ;
5. To display 4 characters starting from 5th character from the string ‘Hippopotamus’
select mid(‘Hippopotamus’, 5, 4);
or
select substring(‘Hippopotamus’, 5, 4);
or
select substr(‘Hippopotamus’, 5, 4);
6. To display position of “Office” in the string “LibreOffice Write”
select instr(“LibreOffice Write”, ‘Office’);
8. (a) To remove the leading and trailing spaces from the string given above.
select trim(“ Category “);
(b) To remove the trailing spaces from the string given above.
select rtrim(“ Category “);
13. To display the 2nd, 3rd and 4th character of string “Windows” upper case.
Select upper(mid(“Windows”, 2,3));
14. To display the first three character of the month name in uppercase from the date “2025-03-29”.
select upper(left(monthname(“2025-03-29”),3));
15. To display last three character of string “First” using mid()/substring() using the negative indexing.
Select mid(“First”, -3);
Questions on Joins:
Machinery Table
MachineID MachineName MachineType PurchaseDate Cost (₹) Status
1 Lathe Machine Industrial 2018-05-20 2,50,000.00 Active
2 Bulldozer Construction 2015-09-15 12,00,000.00 Under Repair
3 CNC Milling Industrial 2015-11-10 7,50,000.00 Active
4 Tractor Agricultural 2017-03-05 6,00,000.00 Active
5 Forklift Warehouse 2018-07-25 4,50,000.00 Retired
6 Excavator Construction 2020-01-14 13,50,000.00 Active
Maintenance Table (Linked to Machinery)
MaintenanceID MachineID MaintenanceDate MCost Technician
101 2 2023-01-10 50,000.00 Rajesh Kumar
102 3 2023-03-15 75,000.00 Arun Mehta
103 4 2023-06-05 32,000.00 Meenakshi S
104 6 2023-09-22 82,000.00 Vikram R
(ii) To display the MachineID, Purchase date and Cost of Maintenance details.
Select machinery.machineID,PurchaseDate, Mcost from machinery, maintenance where
machinery.machineID = maintenance.machineID;
(iii) To display the Machine name , Purchase date and Maintenance date of those machine whose
maintenance happened in June month.
Select machineName, Purchasedate, MaintenanceDate from machinery, maintenance where
machinery.machineID = maintenance,MachineID and monthName(MaintenanceDate) = ‘June’;
(iv) To display Machine Name and Type of those machines which are maintained by “Arun Mehta”.
Select machineName, machineType from Machinery, Maintenance where machinery.machindID
= maintenance.machineID and technician = ‘Arun Mehta’;
(v) To display Purchase year wise maximum cost of maintenance.
Select year(purchasedate), max(mcost) from machinery, maintenance where
machinery.machindID = maintenance.machineID group by year(purchasedate);
(vi) To display machine type wise sum of maintenance cost for whose machines whose sum of
maintenance cost is more than 100000.
Select machineType, sum(mcost) from from machinery, maintenance where
machinery.machindID = maintenance.machineID group by machinType having sum(mcost) >
100000;