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

My SQL Worksheet-1

The document provides a series of SQL exercises focusing on various commands and queries related to database tables. It includes examples of SELECT statements, conditions for filtering data, and correcting erroneous queries. The exercises cover topics such as retrieving specific data, using wildcards, and understanding the structure of SQL queries.

Uploaded by

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

My SQL Worksheet-1

The document provides a series of SQL exercises focusing on various commands and queries related to database tables. It includes examples of SELECT statements, conditions for filtering data, and correcting erroneous queries. The exercises cover topics such as retrieving specific data, using wildcards, and understanding the structure of SQL queries.

Uploaded by

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

Informatics Practices

My SQL Worksheet-6
(DML – SELECT command)
1. Pooja, a students of class XI, created a table "Book". Price is a column of this table. To find the details
of books whose prices have not been entered she wrote the following query:
Select * from Book where Price = NULL;
Select * from book where price is null;

2. The LastName column of a table "Directory" is given below:


Based on this information, find the output of the following
queries:
a) SELECT lastname FROM Directory WHERE lastname like "_a%";

b)SELECT lastname FROM Directory WHERE lastname not like "%a";

A) Lastname
Batra
B) Lastname
Sehgal

3. Consider the table TEACHER given below. Write commands in SQL for (1) to (3) and output for (4)

i. To display all information about teachers of PGT category.


ii. To list the names of female teachers of Hindi department.
iii. To list names, departments and date of hiring of all the teachers in ascending order of date of joining
iv. SELECT DISTINCT(category) FROM teacher;
i. Select * from teacher where category=”PGT”;
ii. select name from gym where gender=”F” and department=”Hindi”;
iii. Select name,department,hiredate from teacher order by hiredate;
iv. DISTINCT(Category)
TGT
PRT
PGT

4. The ltem_No and Cost columna of a table "ITEMS" are given below:
Based on this information, find the output of the following queries:
a) SELECT COST +100 FROM ITEMS WHERE ITEM_NO > 103;
Ans. COST+100
6100
NULL

5. Consider the table Projects given below. Write commands in SOL for i) to iii) and output for iv)

12
i. To display all information about projects of"Medium" ProjSize
ii. To list the ProjSize of projects whose ProjName ends with LITL.
iii. To list ID, Name, Size, and Cost of all the projects in descending order of StartDate.
iv. SELECT DISTINCT ProjSize FROM projects
i. Select * from projects where projsize=”Medium”;
ii. Select projsize from projects where projname like “%LITL”;
iii. Select ID,projName,projSize,cost from projects order by startDate desc;
iv. ProjSize
Medium
Large
Small
6. The Mname Column of a table Members is given below :
Based on the information, find the output of the following queries :
(i) Select Mname from members where mname like "%v" ;
(ii) Select Mname from members where mname like "%e%";

Ans. i) Mname
Hirav
Rajeev
ii) Mname
Sheetal
Rajeev
7. Sarthya, a student of class XI, created a table "RESULT". Grade is one of the column of this table. To
find the details of students whose Grades have not been entered, he wrote the following MySql query,
which did not give the desired result.
SELECT * FROM Result WHERE Grade= "Null";
Help Sarthya to run the query by removing the errors from the query and write the correct Query.
Select * from Result where Grade is null;

8. Consider the table RESULT given below. Write commands in MySql for (i) to (ii)

(i) To list the names of those students, who


have obtained Division as FIRST in the
ascending order of NAME.
(ii) To display a report listing NAME, SUBJECT
and Annual stipend received assuming that the
stipend column has monthly stipend.

i) Select name from Result where division=”First” order by name;


ii) Select name,subject,stipend*12 from Result;

9. Mr. Janak is using a table with following columns :


Name , Class , Course_Id, Course_name
He needs to display names of students, who have not been assigned any stream or have been assigned
Course_name that ends with "economics". He wrote the following command, which did not give the
desired result.
SELECT Name, Class FROM Students WHERE Course name = NULL OR Course name="%economics";
Help Mr. J anak to run the query by removing the error and write the correct query.
SELECT Name, Class FROM Students WHERE Course name IS NULL OR Course name LIKE
"%economics";

13
10. Consider the Table SHOPPE given below. Write command in MySql for (i) to (ii)

(i) To display names of the items whose name starts


with 'C' in ascending order of Price.
(ii) To display Code, Item name and City of the
products whose quantity is less than 100.

i) Select Item from shoppe where item like “C%” order by price;
ii) Select code,item,city from shoppe where qty<100;

11. What is used in the SELECT clause to return all the columns in the table?
* (asterisk) sign

12. In MySQL, Sumit and Fauzia are getting the following outputs of ItemCodes for SELECT statements used
by them on a table named ITEM.(Both have used the SELECT statements on the same table ITEM).
Sumit’s Output Fauzia’s Output
101 101
102 102
101 105
105 107
101
107
Which extra keyword has Fauzia used with SELECT statement to get the above output?
Distinct

13. Consider the table ‘PERSONS’ given below. Write commands in SQL for (i) to (iv) and write output for
(v).

(i) Display the SurNames, FirstNames and Cities


of people residing in Udhamwara city.
(ii) Display the Person Ids (PID), cities and
Pincodes of persons in descending order of Pincodes.
(iii) Display the First Names and cities of all the
females getting Basic salaries above 40000.
(iv) Display First Names and Basic Salaries of all
the persons whose firstnames starts with “G”.
(v) SELECT Surname FROM Persons Where
BasicSalary>=50000;

i) Select surname,firstname,city from persons where city=”Udhamwara”;


ii) Select pid,city,pincode from persons order by pincode desc;
iii) Select firstname,city from persons where gender=”F” and basicSalary>40000;4
iv) Select firstname,basicSalary from persons where firstname like “G%”;
v) Surname
Sharma
Singh
Alvis
14. Mr. Tondon is using table EMP with the following columns.
ECODE,DEPT,ENAME,SALARY
He wants to display all information of employees (from EMP table) in ascending order of ENAME and
within it in ascending order of DEPT. He wrote the following command, which did not show the desired
output.
SELECT * FROM EMP ORDER BY NAME DESC,DEPT;
Rewrite the above query to get the desired output.
SELECT * FROM EMP ORDER BY ENAME,DEPT;
14
15. Consider the following table named "GYM" with details about fitness items being sold in the store. Write
command of SQL for (i) to (ii).

(i) To display the names of all the items whose name starts
with "A".
(ii) To display ICODEs and INAMEs of all items, whose
Brandname is Reliable or Coscore.

i) Select iname from gym where iname like “A%”;


ii) Select ICode,Iname from gym where brandname in(“Reliable”,”Coscore”);

16. Consider the following table named 'SBOP" with details of account holders. Write commands of MySql
for (i) to (ii) and output for (iii).

(i) To display Accountno, Name and DateOfopen of account


holders having transactions more than 8.
(ii) To display all information of account holders whose
transaction value is not mentioned.
(iii) SELECT NAME,BALANCE FROM SBOP WHERE NAME LIKE “%i”;

i) Select AccountNo,Name,Dateofopen from sbop where transaction>8;


ii) Select * from sbop where transaction is null;
iii) Name Balance
Mrs. Sakshi 45000.00

17. When using the LIKE clause, which wildcard symbol represents any sequence of none, one or more
characters ?
%
18. Consider the table FLIGHT given below. Write commands in SQL for (i) to (iv) and output for (v).

(i) Display details of all flights starting from Delhi.


(ii) Display details of flights that have more than 4 number of flights operating.
(iii) Display flight codes, starting place, destination, number of flights in descending order of number
of flights.
(iv) Display destinations along with flight codes of all the destinations starting with ‘A’.
(v) SELECT DISTINCT(NO_STOPS) FROM FLIGHT;
i) Select * from flight where start=”Delhi”;
ii) Select * from flight where no_flights>4;
iii) Select flcode,start,destination,no_flights from flight order by no_flights desc;
iv) Select destination,flcode from flight where destination like “A%”;
v) NO_STOPS
0
1
2
3

19. What will be the output of the following queries on the basis of Employee table:

15
(i) Select Salary+100 from Employee where EmpId='A002';

i) Salary +100
NULL

20. Pranay, who is an Indian, created a table named “Friends” to store his friend’s detail.
Table “Friends” is shown below.
Write commands in SQL for (i) to
(iii) and output for (iv).

i. To display list of all foreigner


friends.
ii. To list name, city and country in descending order of age.
iii. To list name and city of those friends who don’t have an email id.
iv. Select name,country from friends where age>12 and name like ‘A%’;
i. Selct * from friends where country not in(“India”);
ii. Select name,city,country from friends order by age desc;
iii. Select name,city from friends where email_id is null;
iv. Name Country
Alice USA
Angel USA
Alexender Australia

21. Consider the following table named “GARMENT”. Write command of SQL for (i)
to (iv) and output for (v) to (vii).
(i) To display names of those garments that are
available in ‘XL’ size.
(ii) To display codes and names of those garments
that have their names starting with ‘Ladies’.
(iii) To display garment names, codes and prices of
those garments that have
price in the range 1000.00 to 1500.00 (both 1000.00
and 1500.00 included).
(iv) SELECT GNAME FROM GARMENT WHERE SIZE
IN (‘M’, ‘L’) AND PRICE > 1500;
i) Select gname from garment where size=”XL”;
ii) Select gcode,gname from garment where gname like “Ladies%”;
iii) Select gname,gcode,price where price between 1000.00 and 1500.00
iv) Gname
Jeans

22. Consider the table ‘empsalary’.

To select tuples with some salary ,Siddharth has written the following erroneous
SQL
statement:
SELECT ID, Salary FROM empsalary WHERE Salary = something;

SELECT ID, Salary FROM empsalary WHERE Salary is not null;

16
23. Consider the table ‘Employee’.
Write the SQL command to obtain the following output :

Select distinct Location from employee;

24. Table “Emp” is shown below. Write commands in SQL for (i) to (iii) and output for (iv) and (v)
and (vi)
i. To display list of all employees below 25 years old.
ii. To list names and respective salaries in
descending order of salary.
iii. To list names and addresses of those persons who
have ‘Delhi’ in their address.
iv. SELECT Name, Salary FROM Emp where salary
between 50000 and 70000;
v. SELECT Name, phone from emp where phone like
‘99%’;
i. Select * from emp where age<25;
ii. Select name,salary from emp order by salary desc;
iii. Select name,address where address like “%Delhi%”;
iv. Name salary
Siddharth 62000
Karan 65000
v. Name Phone
Chavi 99113423989
Raunaq 99101393576

25. Mrs. Sen entered the following SQL statement to display all Salespersons of the cities “Chennai”
and ‘Mumbai’ from the table ‘Sales’.
Scode Name City SELECT * FROM Sales WHERE
City=‘Chennai’ AND City=‘Mumbai’;
101 Aakriti Mumbai
102 Aman Chennai
103 Banit Delhi
104 Fauzia Mumbai
Rewrite the correct statement, if wrong or write statement is correct.
SELECT * FROM Sales WHERE City=‘Chennai’ OR City=‘Mumbai’;

26. Write commands in SQL for (i) to (iii) and output for (iv).
Table : Store
StoreId Name Location City NoOfEmployees DateOpened SalesAmount
S101 Planetfashion KarolBagh Delhi 7 2015-10-16 300000
S102 Trends Nehru Mumbai 11 2015-08-09 400000
Nagar
S103 Vogue Vikas Delhi 10 2015-06-27 200000
Vihar
S104 Superfashion Defence Delhi 8 2015-02-18 450000
Colony
S105 Rage Bandra Mumbai 5 2015-09-22 600000
(i) To display name, location, city, SalesAmount of stores in descending order of SalesAmount.

17
(ii) To display names of stores along with SalesAmount of those stores that have ‘fashion’ anywhere in
their store names.
(iii) To display Stores names, Location and Date Opened of stores that were opened before 1st March,
2015.
(iv) SELECT distinct city FROM store;
i) Select name,location,city,salesamount from Store order by salesamount desc;
ii) Select name,salesamount from store where name like “%Fashion%”;
iii) Select name,location,dateOpened from store where dateOpened<”2015-03-01”;
iv) CITY
Delhi
Mumbai

27. Which clause would you use with Select to achieve the following:
i.To select the values that match with any value in a list of specified values.
ii.Used to display unrepeated values of a column from a table.
i. IN
ii. DISTINCT

28. Consider the following table:


Table: PharmaDB
Write commands in SQL for (i) to (iii) and output
for (iv):
i. To increase the price of “Amlodipine” by 50.
ii. To display all those medicines whose price is in
the range 100 to 150.
iii. To display the Drug ID, DrugName and
Pharmacy Name of all the records in descending
order of their price.
iv. SELECT RxID, DrugName, Price from
PharmaDB where PharmacyName IN (“Rx Parmacy”, “Raj Medicos”);
i. Update PharmaDB set price=price+50 where drugname=”Amlodipine”;
ii. Select * from pharamdb where price between 100 and 150;
iii. Select DrugID,Drugname,pharmacyName from pharmaDB order by price desc;
iv. RXID DrugName price
R1000 Amlodipine 100.00
R1001 Paracetamol 15.00
R1004 Levocitrezine 110.00

29. Write SQL statement that gives the same output as the following SQL statement but uses ‘IN’ keyword.
SELECT NAME FROM STUDENT WHERE STATE = ‘VA’ ;
SELECT NAME FROM STUDENT WHERE STATE IN(‘VA’);

30. Which one of the following SQL queries will display all Employee records containing the word “Amit”,
regardless of case (whether it was stored as AMIT, Amit, or amit etc.) ?
(i) SELECT * from Employees WHERE EmpName like UPPER ‘%AMIT%’;
(ii) SELECT *from Employees WHERE EmpName like ‘%AMIT%’ or ‘%AMIT%’ OR ‘%amit%’;
(iii) SELECT * from Employees WHERE UPPER (EmpName) like ‘%AMIT%’;
(iii) SELECT * from Employees WHERE UPPER (EmpName) like ‘%AMIT%’;

18
31. Write Answer to (i). Write SQL queries for (ii) to (vii).

Note : Columns SID and DOB contain Sales Person Id and Data of Birth respectively.
(i) Write the data types of SID and DOB columns.
(ii) Display names of Salespersons and their Salaries who have salaries in the range 30000.00 to 40000.00
(iii) To list Names, Phone numbers and DOB (Date of Birth) of Salespersons who were born before 1st
November, 1992.
(iv) To display Names and Salaries of Salespersons in descending order of salary.
(v) To display areas in which Salespersons are working. Duplicate Areas should not be displayed.
(vi) To display SID, Names along with Salaries increased by 500. (Increase of 500 is only to be displayed
and not to be updated in the table)
(vii) To display Names of Salespersons who have the word ‘Kumar’ anywhere in their names.
i. The data type of SID is either char or varchar
ii. Select name,salary from salesperson where salary between 30000.00 and 40000.00;
iii. Select name,phone,dob from salesperson where dob<”1992-11-01”;
iv. Select name,salary from salesperson order by salary desc;
v. Select distinct area from salesperson;
vi. Select sid,name,salary+500 from salesperson;
vii. Select name from salesperson where name like “%Kumar%”;

32. Write the following statement using ‘OR’ logical operator :


SELECT first_name, last_name, subject FROM studentdetails WHERE subject IN (‘Maths’, ‘Science’);
Select first_name, last_name, subject from studentDetails Where subject=”Maths” or
subject=”Science”;

33. Consider the Table “Gym” shown below. Write commands in SQL for (i) to (vi) :

(i) To display Mname, Age, FeeGiven of those members whose fee is above 12,000.
(ii) To display Mcode, Mname, Age of all female members of the Gym with age in descending order.
(iii) To list names of members and their date of admission of those members who joined after 31st
December, 2015.
iv) To display the Mname, FeeGiven of all those members of the Gym whose age is less than 40 and are
monthly type members of the Gym.
(v) To display names of members who have ‘mit’ anywhere in their names. For example : Amit, Samit.
(vi) To display types of memberships available. Duplicate values should not be displayed.
i) Select mname, age, feegiven from gym where feegiven>12000;
ii) Select mcode,mname,age from gym where gender=”Female” order by age desc;
19
iii) Select mname,dtAdmit from gym where dtAdmit>”2015-12-31”;
iv) Select mname,feegiven from gym where age<40 and type=”Monthly”;
v) Select mname from gym where mname like “%mit%”;
vi) Select distinct type from gym;

34. Consider the following table:


Write commands in SQL for (i) to (iv) and output for
(v):
i. To display the details of all those students who
have IP as their optional subject.
ii. To display name, stream and optional of all those
students whose name starts with ‘A’.
iii. To give an increase of 3 in the average of
all those students of humanities section who have
Maths as their optional subject.
iv. To display a name list of all those students
who have average more than 75.
v. Select name from students where optional IN (‘CS’,’IP’);
i. Select * from student where optional=”IP”;
ii. Select name,stream,optional from student where name like “A%”;
iii. Update student set average=average+3 where stream=”Humanities”;
iv. Select name from student where average>75;
v. Name
Shrishti
Aditya
Ritu Raj
Saumya
Ashutosh
Aman

20

You might also like