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

SQL Test 2_CS - Answers

The document contains SQL queries for managing land and machinery databases, including operations such as selecting, inserting, updating, and deleting records. It includes specific queries for displaying land details based on various criteria, creating and modifying a machinery table, and performing maintenance-related queries. The document serves as a comprehensive guide for database manipulation and retrieval of information related to land and machinery.

Uploaded by

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

SQL Test 2_CS - Answers

The document contains SQL queries for managing land and machinery databases, including operations such as selecting, inserting, updating, and deleting records. It includes specific queries for displaying land details based on various criteria, creating and modifying a machinery table, and performing maintenance-related queries. The document serves as a comprehensive guide for database manipulation and retrieval of information related to land and machinery.

Uploaded by

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

Table: LandDetails

Write queries for the following:

1. Display PattaID, Ownername of lands from Chennai


select PattaID, Ownername from landdetails where location = ‘Chennai’;

2. Display the different types of land.


Select distinct landtype from landdetails;

3. Display the PattaID, OnwerName and Survey number of those land whose status is active and are
registered before 2018
select pattaid, ownername, surveynumber from landdetails where status = ‘Active’ and
registrationdate < ‘2018-01-01’;

4. Display the records of those land which registered in the year 2018, 2019 and 2020.
select * from landdetails where registrationdate between ‘2018-0-01’ and ‘2020-12-31’;

5. Display the Owner Name and Location of those lands whose area size is known.
Select ownername, location from landdetails where area is not null;

6. Display the Pattaid, Location and Landtype of whose marketvalue is from 2500000 to 3000000.
select PattaId, location, landtype from landdetails where marketvalue between 2500000 and
300000;

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;

8. Display the no of different types of lands.


Select count(distinct landtype) from landdetails;

9. Display the cardinality of the table.


Select count(*) from landdetails;

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’);

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;

17. Display minimum area size for each type of land.


Select landtype, min(area) from landdetails group by landtype;

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;

19. Display the records in descending order of area size.


Select * from landdetails order by area desc;

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 final cost for all the lands where final cost = marketvalue * area.
Select marketvalue * area as “Final Cost” from landdetails;

Machinery table:

Column Name Data Type Constraints


machine_id INT PRIMARY KEY
name VARCHAR(50) NOT NULL
type VARCHAR(30)
serial_number VARCHAR(50) UNIQUE
purchase_date DATE
cost FLOAT
(i) Write command to create the above table.
Create table machinery(machine_id int primary key, name varchar(50) not null, type
varchar(30), serial_number varchar(50) unique, purchase_date date, cost float);

(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;

(ix) Write command to create a database named “Alphabet”


create database alphabet;

(x) Write command to display all the databases


show databases;

(xi) Write command to show all the tables under the selected database;
show tables;

(xi) Write command to select a database named ‘Beta’


use Beta;
(Xii) Write command to show the above table’s structure
desc machinery
or
describe machinery

Machinery table:

machine_id name type serial_number purchase_date cost


101 Lathe Machine Cutting SN-00123 2022-05-10 25000.50
102 Milling Machine Drilling SN-00456 2021-08-15 45000.75
103 Welding Robot Automation SN-00789 2023-01-20 60000.00
104 Conveyor Belt Material Handling SN-00987 2020-11-30 15000.00
105 CNC Machine Precision SN-01234 2022-09-25 75000.25
Write SQL commands for the following:
1. A new machine, 3D Printer, has been purchased for ₹85,000.00 on 2024-03-15. It belongs to the
Prototyping category and has a unique serial number SN-01567. Write command to add record based
on the above data.(Machine ID = 106)
Insert into machinery values (106, ‘3D Printer’, ‘Prototyping’, ‘ SN-01567’, ‘2024-03-15’,
85000.00);
2. To modify the cost of the machine with serial number SN-00789 (Welding Robot) to ₹65,000.00.
update machinery set cost = 65000.00 where serial_number = ‘SN-00789’;
3. To decrement the cost of all the products by 1200.
update machinery set cost = cost -1200;
4. To increase the cost of all the product by 7%
update machinery set cost = cost*107/100;
5. To remove the records with type as Cutting.
Delete from machinery where type = ‘Cutting’;
6. To remove all the records from the table.
Delete from machinery;
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
Write SQL queries for the following
(i) To display the Machine name, Machine type and maintenance date details.
Select MachineName, Machinetype, maitenancedate from machinery m, maintenance a where
m.machineId = a.machineid;
(ii) To display the MachineID, Purchase date and Cost of Maintenance details.
Select p.machineid, purchasedate,mcost from machinery p, maintenance a where p.machineid =
a.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 m, maintenance a where
m.machine = a.machineID and maintenancedate like ‘____-06-__’;
(iv) To display Machine Name and Type of those machines which are maintained by “Arun Mehta”
select machineName, machinetype from machinery m, maintenance a where m.machine =
a.machineID and technician = ‘Arun Mehta’;
(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 machinery m, maintenance a where m.machine =
a.machineID group by machinetype having sum(mcost) > 100000;

You might also like