0% found this document useful (0 votes)
89 views16 pages

DB Queries

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views16 pages

DB Queries

Uploaded by

Pavan Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

DB Queries :

REquirement SQL Comments


[Link] the dept information from Dept information in
department table SELECT *from dept the sense all the info
[Link] the details of all employees SELECT *from emp
[Link] the name and job for all
employees select ename,job from emp
[Link] name and salary for all
employees select ename,sal from emp sal without comm
[Link] employee number and total select empno,sal+IFnull(comm,0) empno and monthly
salary for each employee from emp sal
select ename,
[Link] empname,annual sal of each ((sal+ifnull(comm,0))*12) as
emp annualsalary from emp
[Link] the names of all employees
who are working in department number select ename from emp where
10 deptno=10
[Link] the names of all employees
working as clerks and drawing a salary select ename from emp where
more than 3000 job like "CLERK" and sal>3000 No results displayed
select empno, ename from emp
[Link] employee number and names where comm!=0 and comm is
for employees who earn commission NOT null
select empno, ename,comm from
[Link] names of employees who do emp where comm=0 OR comm
not earn any commission is null
[Link] the names of employees who select ename from emp where
are working as clerk , salesman or (job="CLERK" or no data just check
analyst and drawing a salary more than job="SALESMAN" or for sal>1000 instead
3000 job="ANALYST") and sal>3000 of 3000
[Link] the names of employees who select ename,hiredate from emp
are working in the company for the past where (CURRENT_DATE- dispalying hire date
5 years hiredate)>=5 also for clarity
SELECT ename,hiredate from
emp where hiredate NOT
[Link] the list of employees who BETWEEN cAST('1990-06-30'
have joined the company before 30 th AS date) AND cast('1990-12-31' displaying hire date
june 90 or after 31 st dec 90 as date) also for clarity
select CURRENT_DATE from
[Link] current date DUAL
[Link] the list of users in your select user,HOST from
database (using log table) [Link]
[Link] the names of all tables from
the current user SHOW TABLES
[Link] the name of the current user SELECT CURRENT_USER
select ename,job,deptno from
[Link] the names of employees emp where deptno in(10,20,40)
working in department number 10 or 20 OR job
or 40 or employees working as clerks , in('CLERK','SALESMAN','ANALY
salesman or analyst ST')
[Link] employee name from
employees whose name starts with select ename from emp where
alphabet S ename like "s%"
[Link] employee name from
employees whose name ends with select ename from emp where
alphabet S ename like "%s"
[Link] the names of employees
whose names have sencond alphabet A select ename from emp where
in their names ename like'_A%'
[Link] the names of employees select ename from emp where
whose names has 5 character length length(ename)=5
[Link] the names of employees select ename from emp where
whose is not working as manager job not like "MANAGER"
[Link] the names of employees who select ename,job from emp
are not working as SALESMAN or CLERK where job not IN ('SALESMAN',
or ANALYST 'CLERK' , 'ANALYST')
[Link] all rows from emp table. The
system should wait after every screen full
of information
[Link] the total number of
employees working in the company select count(empno) from emp
[Link] the total salary and total select ename,sal,comm from
commission to all employees emp
[Link] the maximum salary from
emp table select max(sal) from emp
[Link] the minimum salary from emp
table select min(sal) from emp
[Link] the average salary from emp
table select avg(sal) from emp
[Link] the maximum salary being select max(sal) from emp where
paid to CLERK job="CLERK"
[Link] the maximum salary being select max(sal) from emp where
paid in dept no 20 deptno=20
[Link] the minimum salary being select min(sal) from emp where
paid to any SALESMAN job="SALESMAN"
[Link] the average salary drawn by select AVG(sal) from emp where
managers job="MANAGER"
[Link] the total salary drawn by select sum(sal) from emp where gives NULL data try
analyst working in dept no 40 job="ANALYST" and deptno=40 with dept 20
[Link] the names of employees in
order of salary i.e. the name of the
employee earning lowest salary shoud select ename,sal from emp order
appear first by sal asc
[Link] the names of employees in select ename,sal from emp order
descending order of salary by sal desc
[Link] the details from emp table in select ename from emp order by
order of emp name ename asc
[Link] empnno,ename,deptno and
sal. Sort the output first based on name select empno,ename,deptno,sal first name is ordered,
and within name by deptno and witdhin from emp order by sal then dept and then
deptno by sal; ASC,deptno ASC,ename ASC sal
40) Display the name of employees along select ename,
with their annual salary(sal*12),the name ((sal+ifnull(comm,0))*12) as
of the employee earning highest annual annualsal from emp order by
salary should appear first? annualsal desc

41) Display
name,salary,Hra,pf,da,TotalSalary for select ename,sal,(sal*15/100) as
each employee,The out put should be in HRA, (sal*10/100) as DA,
the order of total salary ,hra 15% of (sal*5/100) as PF, (sal+
salary ,DA 10% of salary .pf 5% salary (sal*15/100)+(sal*10/100)-
Total Salary will be (salary+hra+da)-pf? (sal*5/100)) as totsal from emp

42) Display Department numbers and select deptno,count(empno) as


total number of employees working in empcount from emp group by
each Department? deptno
43. dispay each job group and count of select job,count(empno) as
employee working under it empcount from emp group by job
44)Display department numbers and select deptno,sum(sal) as totsal
Total Salary for each Department? from emp group by deptno
45)Display department numbers and select deptno,max(sal) as totsal
Maximum Salary from each Department? from emp group by deptno
46)Display various jobs and Total Salary select job,sum(sal) as totsal from
for each job? emp group by job
47)Display each job along with min of select job,min(sal) as totsal from
salary being paid in each job group? emp group by job
48) Display the department Number with select deptno from emp group by
more than three employees in each deptno HAVING
department? COUNT(empno)>3
49) Display various jobs along with total select job,sum(sal) as totsal from
salary for each of the job where total emp group by job having no data for 40000 ,
salary is greater than 40000? totsal>40000 use 5000
50) Display the various jobs along with select job,count(empno) as
total number of employees in each empcount from emp group by job
[Link] HAVING COUNT(empno)>3
output should contain only those jobs
with more than three employees?
51) Display the name of employees who select ename from emp where
earn Highest Salary? sal=(select max(sal) from emp)
52) Display the employee Number and select empno,ename from emp
name for employee working as clerk and where sal=(select max(sal) from
earning highest salary among the clerks? emp where job="CLERK")
select ename,sal,job from emp
53) Display the names of salesman who where job="SALESMAN" and
earns a salary more than the Highest sal>(select max(sal) from emp displaying sal and
Salary of the clerk? where job="CLERK") job for clarity
select ename,sal,job from emp
54) Display the names of clerks who earn where job="CLERK" and
a salary more than the lowest Salary of sal>(select min(sal) from emp
any salesman? where job="SALESMAN")
55) Display the names of employees who select ename from emp where
earn a salary more than that of jones or sal>(select sal from emp where
that of salary greater than that of scott? ename like 'JONES' or 'SCOTT')
56) Display the names of employees who select ename,sal from emp
earn Highest salary in their respective where sal in (select max(sal) displaying sal aslo
departments? from emp group by deptno) for clarity
57) Display the names of employees who select ename,sal from emp
earn Highest salaries in their respective where sal in (select max(sal)
job Groups? from emp group by job)
select ename from emp where
deptno in (select deptno from
58) Display employee names who are dept where
working in Accounting department? dname="Accounting")
select ename from emp where
59) Display the employee names who are deptno in (select deptno from
Working in Chicago? dept where location="Chicago")
select job,sum(sal) as totsal from
emp group by job HAVING
60) Display the job groups having Total sum(sal)>(SELECT max(sal)
Salary greater than the maximum salary from emp where
for Managers? job="MANAGER")

select COUNT(empno) as
empcount,empno ,ename,job,mg
r from emp where mgr is NOT
null group by mgr
select mgr,count(empno) from
emp WHERE mgr IN (SELECT
empno FROM emp WHERE
job="MANAGER" )GROUP BY
count of employees under manager mgr

61) Display the names of employees from select ename from emp where
department number 10 with salary deptno=10 and sal>any(select
greater than that of ANY employee max(sal) from emp where deptno
working in other departments? not in (10))
62) Display the names of employees from select ename from emp where
department number 10 with salary deptno=10 and sal>(select
greater than that of ALL employee max(sal) from emp where deptno
working in other departments? not in (10))
63) Display the names of mployees in select upper(ename) from emp
Upper Case?
64) Display the names of employees in
Lower Case? select lower(ename) from emp
select
CONCAT(UCASE(SUBSTRING(`
ename`, 1,
65) Display the names of employees in 1)),LOWER(SUBSTRING(`enam
Proper case? e`, 2))) as ename from emp
66) Find the length of your name using select length('pavan kumar') from
Appropriate Function? DUAL
67) Display the length of all the employee select length(ename)
names? enamelength from emp
68) Display the name of employee select
Concatinate with Employee Number? concat(ename,empno)from emp
69) Use appropriate function and extract
3 characters starting from 2 characters
from the following string 'Oracle' i.e., the select substring('oracle',3,2) from
out put should be ac? DUAL
70) Find the first occurance of character select locate('a','Computer
a from the following string Computer Maintenance Corporation') from
Maintenance Corporation? DUAL
71) Replace every occurance of alphabet
A with B in the string .Alliens (Use select REPLACE('Alliens ','A','B')
Translate function)? from DUAL
72) Display the information from the select ename,
employee table . where ever job Manager REPLACE(job,'MANAGER','Boss
is found it should be displayed as Boss? ') as job from emp
SELECT
SELECT ename,empno,encod
73) Display empno,ename,deptno from ename,empno,decode(deptno,de e(deptno,[Link]
tvsemp table. Instead of display [Link]) from emp join dept e) from emp join
department numbers USING(deptno) dept USING(deptno)
display the related department name(Use
decode function)?
To Display age in
days and age like
(28 yr 9 mth 28 dy)
select
DATE_FORMAT(FR
OM_DAYS(DATEDI
FF(CURRENT_DAT
E,'1993-05-02')),'%y
yr %c mth %e dy')
AS age,
select DATEDIFF(CURRE
DATEDIFF(CURRENT_DATE,'1 NT_DATE,'1993-05-
74) Display your Age in Days? 993-05-02') days from DUAL 02') days from DUAL
75) Display your Age in Months? SELECT
FLOOR((DATEDIFF(CURRENT_
DATE,'1993-05-02')/365)*12)+
FLOOR((DATEDIFF(CURDATE()
,'1993-05-02')/365 -
FLOOR(DATEDIFF(CURDATE(),
'1993-05-02')/365))* 12)
TotMonths FROM dual
select
Floor(DATEDIFF(CURRENT_DA
TE,'1993-05-02')/(365) )days
75)Display age in years from DUAL
day name
SELECT
dayname(CURRENT
_DATE) from DUAL
SELECT
monthname(CURRE
NT_DATE) from
DUAL
Sunday 29th
January 2017-
SELECT
date_format(CURRE
NT_DATE,'%W %D
%M %Y') from DUAL
SELECT
SELECT STR_TO_DATE('18,
76) Display current date as 15th August date_format(CURRENT_DATE,' 05,2009','%d,%m,
Friday Nineteen Ninety Seven? %W %D %M %Y') from DUAL %Y');
77) Display the following output for each
row from tvs emp table?
select concat(ename," HAS
JOINED THE COMPANY ON ")
as
title ,date_format(HIREDATE,'%
78) Scott has joined the company on 13th W %D %M %Y') d from EMP
August nineteen ninety? where hiredate='1996-03-05'
SELECT
DATE_ADD(CURDA
TE(), INTERVAL (9 -
IF(DAYOFWEEK(CU
SELECT RDATE())=1, 3,
DATE_ADD(CURDATE(), DAYOFWEEK(CUR
79) Find the nearest Saturday after INTERVAL (6) DAY) as DATE()))) DAY) as
Current date? NEXTSAT NEXTSAT
SELECT CURRENT_TIME from
80) Display the current time? DUAL
select
81) Display the date three months before date_add(CURRENT_DATE,INT
the Current date? ERVAL(-3)month) from dual
select job from emp where
82) Display the common jobs from deptno=10 and job in(select job
department number 10 and 20? from emp where deptno=20)
83) Display the jobs found in department SELECT DISTINCT(job) from
10 and 20 Eliminate duplicate jobs? emp where deptno in (10,20)
84) Display the jobs which are unique to SELECT DISTINCT(job) from
department 10? emp where deptno=10
select
[Link],[Link]
from emp,emp e
where
85) Display the details of those select empno,ename,mgr from [Link]=[Link]
employees who do not have any person emp where empno not in (select group by [Link]
working under him? ifnull(mgr,0) from emp) having count(*)=1
select * from emp where
deptno=(select deptno from dept
where dname="Sales") And sal
BETWEEN (SELECT losal from
86) Display the details of those salgrade where grade=3)and
employees who are in sales department (SELECT hisal from salgrade
and grade is 3? where grade=3)
select ename from emp where
87) display those who are not managers job!="MANAGER"
88)select emp whose name is not less select ename from emp where
than 4 characters length(ename)>4
89) Display those department whose
name start with"S" while location name select * from dept where dname
ends with "K"? like'S%' and location like'%K' No data
select ename,mgr from emp
90) Display those employees whose where mgr in (select empno from
manager name is Jones? emp where ename="JONES")
91) Display those employees whose
salary is more than 3000 after giving select ename from emp where
20% increment? (sal+sal*(20/100))>3000
92) Display all employees with their select [Link],[Link] from
department names? emp e join dept d using (deptno)
select ename from emp where
93)display employees who are in sales deptno in (select deptno from
dept dept where dname="Sales" )
select [Link],
94) Display employee name,dept [Link],sal,comm from emp e
name,salary,and commission for those join dept d on [Link]=[Link]
sal in between 2000 to 5000 while where sal between 2000 And
location is Chicago? 5000 and [Link]="Chicago"
select [Link] from
95) Display those employees whose emp e,emp p where
salary is greater than his managers [Link]=[Link] and
salary? [Link]=[Link];
select * from emp e1
96) Display those employees who are select [Link] from emp e,emp join emp e2 on
working in the same dept where his p where [Link]=[Link] and [Link] = [Link]
manager is work? [Link]>[Link] and [Link] > [Link]
select ename,mgr from emp
97) Display those employees who are not where mgr in (select empno from
working under any Manager? emp where job!="MANAGER")
98) Display the grade and employees select ename,grade from
name for the deptno 10 or 30 but grade emp,salgrade where deptno
is not 4 while joined the company before in(10,30) and grade<>4 and
31-DEC-82? hiredate<'31-DEC-82'
99) Update the salary of each employee update emp set
by 10% increment who are not eligible for sal=(sal+sal*10/100) where
commission? comm is null
100) Delete those employees who joined delete from emp where
the company before 31-Dec-82 while hiredate<'31-dec-82' and deptno
their department Location is New York or in(select deptno from dept where
Chicago? location='New York' or 'Chicago')
select [Link],[Link],
101) Display employee [Link],[Link] from emp e
name ,job,deptname,loc for all who are join dept d using (deptno) where
working as manager? [Link]="MANAGER"
select [Link] as
ename ,[Link] as mgr from
102) Display those employees whose emp e join emp m on
manager name is jones and also display [Link]=[Link] where
their manager name? [Link]='JONES'
select ename,grade,hisal from
103) Display name and salary of ford if emp,salgrade where
his salary is equal to hisal of his grade? ename='FORD' and hisal=sal
SELECT
[Link],[Link],DN
AME,[Link],
GRADE FROM
EMP,EMP
E,SALGRADE,DEPT

select WHERE [Link]


[Link] ,[Link],dname,[Link] BETWEEN LOSAL
as mgr,grade from emp e,emp AND HISAL AND
m,salgrade,dept where [Link] [Link]=[Link]
104) Display employee = [Link] and [Link] BETWEEN R AND
name ,job,deptname,his manager losal and hisal and [Link]=DEP
name ,his grade and make an under [Link]=[Link] order by [Link] ORDER
department wise? dname BY DNAME
select ename,job,
sal,grade,dname from
105) List out all the employee emp ,salgrade,dept where job!
names ,job,salary,grade and deptname ='CLERK' and [Link] between
for every one in a company except losal and hisal and
'CLERK' . Sort on salary display the [Link]=[Link] order
highest salary? by sal asc
106) Display employee name,job abd his select [Link],[Link],[Link] select
manager .Display also employees who as mgr from emp e join emp m [Link],[Link],[Link]
are with out managers? on [Link]=[Link] me as mgr from emp
union select ename,job,mgr from e join emp m on
emp where mgr is null [Link]=[Link]
UNION select
ename,job ,mgr from
emp where empno
not in (select
ifnull(mgr,0) from
emp)
107) Display Top 5 employee of a
Company? select ename from emp limit 5
108) Display the names of those
employees who are getting the highest select ename from emp where
salary? sal=(select max(sal) from emp)
109) Display those employees whose select ename from emp where
salary is equal to average of maximum sal=(select (max(sal)+MIN(sal))/2
and minimum? from emp)
select count(empno)as
110) Select count of employees in each count,deptno from emp group by
department where count >3? deptno having count(*)>3
select dname from emp join dept
111) Display dname where atleast three using(deptno) group by deptno
are working and display only deptname? having count(deptno)>=3
112) Display name of those managers select ename from emp where
name whose salary is more than average job="MANAGER" and sal>(select
salary of Company? avg(sal) from emp )
select avg(sal) as
select ename from emp where empavgsal,mgr from
113) Display those managers name job="MANAGER" and sal>(select emp where mgr in
whose salary is more than average salary avg([Link]) from emp e join emp (select empno from
salary of his employees? m on [Link]=[Link]) emp) group by mgr
select
ename,sal,comm,sal
+nvl(comm,0) as
NetPay from emp
114) Display employee name,sal,comm where
and netpay for those employees whose sal+nvl(comm,0)
netpay is greater than or equal to any >any (select sal from
other employee salary of the company? emp)
115) Display those employees whose
salary is less than his manager but more
than salary of other managers?
116) Display all employees names with SELECT ename,(SELECT
total sal of company with each employee SUM(sal) FROM emp) FROM
name? emp
117) Find the last 5(least) employees of
company?
118) Find out the number of employees
whose salary is greater than their
managers salary?
119) Display the manager who are not
working under president but they are
working under any other manager?
120) Delete those department where no
employee working?
121) Delete those records from emp select ename from emp where
table whose deptno not available in dept deptno not in (select dept no
table? from dept )
select ename from emp where
122) Display those enames whose salary sal not in between (select losal
is out of grade available in salgrade from salgrade) and (select hisal
table? from salgrade)
123) Display employee name,sal,comm
and whose netpay is greater than any
other in the company?
SELECT *
date_add(HIREDAT
124) Display name of those employees SELECT * from emp where E,INTERVAL (12*30)
who are going to retire 31-Dec-99 if date_add(HIREDATE,INTERVAL month) <= '1999-12-
maximum job period is 30 years? (12*30) month) = '1999-12-31' 31'
125) Display those employees whose select ename from emp where
salary is odd value? mod(sal,2)!=0
126) Display those employees whose select ename,floor(sal) from emp
salary contains atleast 3 digits? where length(floor(sal))>3
127) Display those employees who joined select ename from emp where
in the company in the month of Dec? hiredate like '%12%'
128) Display those employees whose select ename from emp where
name contains A? ename like'%A%'
129) Display those employees whose
deptno is available in salary?
130) Display those employees whose first
2 characters from hiredate - last 2
characters sal?

Database testing
https://
[Link]/watch?
v=W_fH6CqiTDU

[Link]
spreadsheets/d/
1WBljrmVijkT3da6QbqDp
qA2Ty9RgosqcvFwDnX4
Queries 5tGc/edit
Tables CREATE TABLE salgrade(
grade int(4) not null primary key,
losal decimal(10,2),
hisal decimal(10,2));

CREATE TABLE dept(


deptno int(2) not null primary key,
dname varchar(50) not null,
location varchar(50) not null);
CREATE TABLE emp(
empno int(4) not null primary key,
ename varchar(50) not null,
job varchar(50) not null,
mgr int(4),
hiredate date,
sal decimal(10,2),
comm decimal(10,2),
deptno int(2) not null);
insert into dept values
(10,'Accounting','New York');
insert into dept values
(20,'Research','Dallas');
insert into dept values
(30,'Sales','Chicago');
insert into dept values
(40,'Operations','Boston');
-------------------------------------------------------
---
insert into emp values
(7369,'SMITH','CLERK',7902,'93/6/13',80
0,0.00,20);
insert into emp values
(7499,'ALLEN','SALESMAN',7698,'98/8/1
5',1600,300,30);
insert into emp values
(7521,'WARD','SALESMAN',7698,'96/3/2
6',1250,500,30);
insert into emp values
(7566,'JONES','MANAGER',7839,'95/10/
31',2975,null,20);
insert into emp values
(7698,'BLAKE','MANAGER',7839,'92/6/11
',2850,null,30);
insert into emp values
(7782,'CLARK','MANAGER',7839,'93/5/1
4',2450,null,10);
insert into emp values
(7788,'SCOTT','ANALYST',7566,'96/3/5',
3000,null,20);
insert into emp values
(7839,'KING','PRESIDENT',null,'90/6/9',5
000,0,10);
insert into emp values
(7844,'TURNER','SALESMAN',7698,'95/6
/4',1500,0,30);
insert into emp values
(7876,'ADAMS','CLERK',7788,'99/6/4',11
00,null,20);
insert into emp values
(7900,'JAMES','CLERK',7698,'00/6/23',95
0,null,30);
insert into emp values
(7934,'MILLER','CLERK',7782,'00/1/21',1
300,null,10);
insert into emp values
(7902,'FORD','ANALYST',7566,'97/12/5',
3000,null,20);
insert into emp values
(7654,'MARTIN','SALESMAN',7698,'98/1
2/5',1250,1400,30);
-------------------------------------------------------
---
insert into salgrade values (1,700,1200);
insert into salgrade values (2,1201,1400);

insert into salgrade values (3,1401,2000);

insert into salgrade values (4,2001,3000);

insert into salgrade values


(5,3001,99999);
Install XAMPP for [Link]
Software Creating database [Link]
[Link]
Data base testing tutorial [Link]
http://
[Link].
com/database-testing-
process/
Joins:
-----------
select emp1.emp_name,emp2.o_sts from
emp1 JOIN emp2 ON
emp1.emp_id=emp2.emp_id
select emp1.emp_name,emp2.o_sts from
emp1 LEFT JOIN emp2 ON
emp1.emp_id=emp2.emp_id
select emp1.emp_name from emp1 FULL
OUTER JOIN emp2 ON
emp1.emp_id=emp2.emp_id order by
emp2.o_id

joining 3 tables:

SELECT emp_name,sup_name from emp1


JOIN emp2 USING (emp_id) join suplier
USING (o_id)
SELECT emp1.*,emp2.*,suplier.*,prod.*
from emp1 JOIN emp2 USING (emp_id) join
suplier USING (o_id)join prod USING
(sup_id)

SELECT emp_name, dept_name FROM


Employee e JOIN Register r ON
e.emp_id=r.emp_id JOIN Department d ON
r.dept_id=d.dept_id;

SELECT TableA.*, TableB.*, TableC.*,


TableD.* FROM (TableB INNER JOIN
TableA
ON [Link]= [Link])
INNER JOIN TableC ON([Link]=
[Link])
INNER JOIN TableA ta ON([Link]=
[Link])
WHERE (DATE([Link])=date(now()))

foreign key after creating table


--------------------------------
alter table suplier add CONSTRAINT fk
FOREIGN KEY(o_id) REFERENCES
emp2(o_id)

updating multiple fields at a time :


update prod set
prod_id=1,prod_det='btm',sup_id=1

update common value in for all records in a


column :
update emp set column_name=1

inserting multiple records in table:


insert into prod values(3,'btm',3),
(4,'krpuram',4)

retrieving data by checking condition on


different tables without using joins:
select emp_name from emp1 where emp_id
IN(SELECT emp_id from emp2 where o_id
IN(select o_id from suplier where
sup_pro="ios" ))

group by:
#ERROR!
#ERROR!
SELECT * FROM `emp1` group by emp_id
having emp_id=1
distinct:
select DISTINCT(pro_id) from prod
displays single field with distinct values

creating duplicate table:(copying all data


from one table to another)
INSERT INTO studentsbkp SELECT *
FROM students WHERE Student_id IN
(SELECT Student_id FROM students)

SELECT emp_name from emp1 WHERE


emp_id=(SELECT emp_id from emp2 where
o_id=1)

count:counts all the records in the table


SELECT count(*) from emp1

Order by:
SELECT * FROM `emp1` order BY
emp_name ASC

like:
SELECT * FROM `emp1` order BY
emp_name ASC

UNION:combines two table data if the no of


columns in the two tables matches and
displaying data in vertical manner
SELECT * from emp2 UNION SELECT *
from prod

Exists: existing info will display


select emp_name from emp1 where
EXISTS (SELECT emp_id from emp2)

teams having more than one lead


SELECT team_name FROM team_table
where
team_id IN (select team_id from emp_base
table where count(team_id)>1 SELECT
team_id from emp_base_table where
designation_id =
(SELECT designation_id
from designation_table where designation
like 'Lead')) and count(team_id)>2

SELECT (team_name) from team_table T


JOIN emp_base_table E USING (team_id)
JOIN designation_table D USING
(designation_id) where [Link] like
'Lead' group by team_name having
COUNT(team_name)>1

teams having leads:


SELECT team_name FROM team_table
where
team_id IN ( SELECT team_id from
emp_base_table where designation_id =
(SELECT designation_id
from designation_table where designation
like 'Lead'))

SELECT DISTINCT(page_title) as title,


count(page_title) AS count
FROM page_title
GROUP BY page_title
HAVING count > 1

SELECT DISTINCT(page_title) as title,


count(page_title) AS count
FROM page_title
GROUP BY page_title
HAVING count > 1
This will return a result like this:

title count
Buy a green widget 2
Buy a blue widget 3
About blue widgets 2

SELECT DISTINCT(designation_id) as
desig, count(designation_id) AS count
FROM emp_base_table GROUP BY
designation_id HAVING count > 1

Displaying second nax salary :


SELECT Salary FROM Employee where Select max(salary)from
salary = (select DISTINCT salary from emp emp where salary
order by salary LIMIT 1,1 ) <(select max(salary) from
emp

employee who is getting max salary:


select emp_name from emp_base_table
where emp_id=(select emp_id from
salary_table where salary=(select
max(salary) from salary_table))
WHERE Salary NOT IN (SELECT
MAX(Salary) FROM Employee )

Find the designation of highest paid salary:


select designation from designation_table
where designation_id=(select designation_id
from emp_base_table where emp_id=(select
emp_id from salary_table where
salary=(select max(salary) from
salary_table)))

find resource allocated to the highest sal


employee:
select resource_types from resource_table
where emp_id=(select emp_id from
emp_base_table where emp_id=(select
emp_id from salary_table where
salary=(select max(salary) from
salary_table)))

You might also like