SQL Test 2_CS - Answers
SQL Test 2_CS - Answers
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;
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%”;
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 final cost for all the lands where final cost = marketvalue * area.
Select marketvalue * area as “Final Cost” from landdetails;
Machinery table:
(ii) Write command to remove the primary key from the table.
Alter table machinery drop primary key;
(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: