Assignment#3 (Sol)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Assignment#3

Given is the table of ‘Teacher’. Do as directed.


Teacher

Tid firstName lastName Dob Gender Qualification salary City


t1 Ali Muhammad 1984-10-15 Male Ph D 85000 RWP
t2 Shahida Jahanzaib 1990-05-03 Female MS 70000 ISB
t4 Faisal Hammad 1988-10-04 Male Ph D 35000 LHR
t5 Ayesha Mukhtar 1991-05-04 Female Ph D 90000 ISB
t6 Nadeem Afzal 1985-06-03 Male MS 95000 LHR
t7 Shahid Abid 1989-07-01 Male MCS 92000 KHI
a) Write down the SQL Queries for following statements.
1) Display details of all Teachers whose qualification is MS or Ph D without using in operator.
SELECT * FROM TEACHER WHERE QUALIFICATION = ‘MS’ OR QUALIFICA-
TION= ‘PH D’
2) Display full name of all those teachers whose salary is less than equal to 85000 and greater than
equal to 40000 without using relational operators.
SELECT FISTNAME+ LASTNAME AS [FULL NAME] FROM TEACHER WHERE
SALARY BETWEEN 40000 AND 85000.
3) Display all those teachers who neither lives in rwp nor isb nor khi without using ‘in’ and ‘like’
operator.
SELECT * FROM TEACHER WHERE CITY != ‘RWP’ and CITY != ‘isb’ and CITY !
= ‘khi’
4) Display all those teachers whose firstName’s second letter and second last letter is ‘a’.
SELECT * FROM TEACHER WHERE fistName like ‘_a%a_’
5) Display first name of all Ph d teachers by adding ‘Mr.’ at start.
Select ‘Mr.’+ firstName from teacher where qualification= ‘ph d’ and gender = ‘Male’
6) Display average salary of female teachers using aggregate function.
Select AVG (salary) from teacher where gender = ‘female’
7) Display count of teachers for each qualification.
Select count (*) , qualification from teacher group by qualification
8) Display all those teachers whose city is either isb or khi and gender is male without using in op -
erator.
Select * from teacher where city = ‘isb’ or city= ‘khi’ and gender= ‘male’
9) Display firstName and lastName of all teachers in such a way that youngest teachers should be
shown on the top.
Select firstName, lastName from teacher order by age asc
10) Display of all those teachers whose qualification either ‘MS’ or ‘Ph D’ and last name must end
on vowel letter.
Select * from teacher where qualification in (‘ms’, ‘ph d’) and lastName like ‘%[aeiou]’
11) Display details of all male teachers who lives in lhr or khi.
Select * from teacher where gender = ‘male’ and city in (‘lhr’ , ‘khi’)
12) Display count of all teachers whose salary is greater than 70000 or city is isb.
Select count(*) from teacher where salary > 70000 or city= ‘isb’
13) Display details of top 5 teachers who lives in khi.
Select top 5 * from teacher where city= ‘khi’
14) Display all those teachers whose first Name consist of less than 4 characters.
Select * from teacher where first Name like ‘_’ or first Name like ‘__’ or first Name like
‘___’
15) Display details of all those teachers whose gender is male and salary is below 50000.
Select * from Teacher where gender = ‘male’ and salary <50000.
16) Display all those teachers whose first Name consist of exactly 5 characters and at least one ‘a’
at any position.
Select * from Teacher where firstName like ‘_____’ and firstName like ‘%a%’
17) Display all those teachers whose first Name consist of at least 4 characters.
Select * from Teacher where firstName like ‘____%’
18) Display unique city of teachers whose first Name start either ‘a’ or ‘r’ or ‘m’ or ‘s’ and must
end on vowel letter.
Select distinct city from teacher where firstName like ‘[arms]%[aeiou]’
19) Show all those teacher whose salary is 35000 after receiving 10 percent increment in his/her
salary.
Select * from teacher where salary +salary*0.1 > 35000
20) Show qualification and total number of teacher for each qualification whose total number of
teacher for each city is greater than equal to ‘2’.
Select count (*), qualification from teacher group by qualification having count (*) >2
b) Consider ‘Teacher’ table given above and draw result for the following SQL queries.
1) Select distinct city from teacher where qualification not in
('BSCS', 'BSIT');
City
ISB
KHI
LHR
RWP

2) Select qualification, count(*) from teacher group by qualifica-


tion;
qualification Total_Teacher
MCS 1
MS 2
PH D 3

3) Select firstName, Gender from teacher where salary not between


30000 and 35000;
firstName Gender
Ali Male
Shahida Female
Ayesha Female
Nadeem Male
Shahid Male

4) Select top 3 tid ,Salary from teacher where lastName not like
'[Muhammad]%' order by dob asc;
Tid LastName

T2
Jahanzaib

5) Select firstName from teacher where gender = 'male' or salary


between 40000 and 60000
firstName

6) Select * from teacher where city=’wrp’ or ‘bsi’;


Tid firstName lastName Dob Gender Qualification salary City

7) Select firstName, lastName from teacher where age >20 and age>30;
firstName lastName
Ali Muhammad
Shahida Jahanzaib
Faisal Hammad
Ayesha Mukhtar
Nadeem Afzal
Shahid Abid

8) Select * from teacher where age<20 or age>30;


Tid firstName lastName Dob Gender Qualification salary City
t1 Ali Muhammad 1984-10-15 Male Ph D 85000 RWP
t2 Shahida Jahanzaib 1990-05-03 Female MS 70000 ISB
t4 Faisal Hammad 1988-10-04 Male Ph D 35000 LHR
t5 Ayesha Mukhtar 1991-05-04 Female Ph D 90000 ISB
t6 Nadeem Afzal 1985-06-03 Male MS 95000 LHR
t7 Shahid Abid 1989-07-01 Male MCS 92000 KHI

9) Select top 3 qualification, salary from teacher where city


in(‘rwp’, ‘isb’,’mulatn’) order by salary asc.
Qualification salary
Ph D 35000
MS 70000
Ph D 85000

10) Select * from teacher where firstName not like ‘[^aeiou]%’;


Tid firstName lastName Dob Gender Qualification salary City
t1 Ali Muhammad 1984-10-15 Male Ph D 85000 RWP
t5 Ayesha Mukhtar 1991-05-04 Female Ph D 90000 ISB

You might also like