2 Tables will be there
Namely Employee having columns like Empid,Empname,Salary,Mgrid.
Phone Table having Empid and Phone number.
Based on these some questions like this
1) select all the Employees who does not have phone?
2)Dispaly all managers from table(Manager id is same as Empid)
3)How to know How many tables contain Empno as a column in database?
4)Find duplicate rows in a table or if we have table with one column which
has many records which are not distinct. How to find out the distinct
values from that column and number of times it's repeated?
5) How to delete the rows which are duplicate?(Don't remove both duplicate
records.)
6)How to find the 6th highest salary?
Select MIN(s.salary) FROM
(SELECT TOP 6 salary FROM Employee ORDER BY salary DESC)s
How to know How many tables contain Empno as a column in
database?
SELECT DISTINCT NAME FROM SYSOBJECTS WHERE ID IN (SELECT ID FROM SYSCOLUMNS WHERE NAME = 'EMPNO')
SELECT COUNT(DISTINCT NAME)COUNT FROM SYSOBJECTS WHERE ID IN (SELECT ID FROM SYSCOLUMNS WHERE NAME = 'DEPOSITNO') AND NAME NOT LIKE 'SYNCOBJ%'
1)select all the Employees who does not have phone?
SELECT * FROM EMPLOY WHERE EMPID NOT IN(SELECT EMPID FROM PHONE)
--2)Dispaly all managers from table(Manager id is same as Empid)
SELECT * FROM EMPLOY E WHERE E.EMPID=E.MGRID
--3)How to know How many tables contain Empno as a column in database?
SELECT name FROM sys.objects WHERE OBJECT_ID IN (SELECT object_id FROM sys.columns WHERE NAME like 'EMPNO')
--4)Find duplicate rows in a table or
-- if we have table with one column which has many records which are not distinct. How to find out the distinct values from that column and number of times its repeated
SELECT * FROM (
SELECT *,ROW_NUMBER()OVER(PARTITION BY EMPNAME ORDER BY EMPID ) AS NUMS FROM EMPLOY ) A WHERE NUMS>1
CREATE TABLE ONE(ID INT)
INSERT INTO ONE VALUES('1'),('2'),('3'),('4'),('5'),('1'),('2'),('3'),('4'),('5'),('1'),('2'),('3'),('4'),('5')
SELECT ID,COUNT(*) FROM ONE group by ID having COUNT(*)>1
--5) How to delete the rows which are duplicate?(Don't remove both duplicate records.)
WITH TA
AS
(
SELECT EMPID,EMPNAME,ROW_NUMBER() OVER(PARTITION BY EMPNAME ORDER BY SAL DESC ) AS Nums
FROM EMPLOY
)
DELETE FROM TA WHERE Nums>1
WITH AT
AS
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY PHONENUMBER ORDER BY EMPID DESC)AS PHONENUMS FROM PHONE
)
DELETE FROM AT WHERE PHONENUMS>1
--6)how to Find 6th highest sal ?
SELECT MIN(SAL) FROM EMPLOY WHERE SAL IN(
SELECT DISTINCT TOP(6) SAL FROM EMPLOY ORDER BY SAL DESC
)
SELECT TOP 1 SAL FROM (SELECT DISTINCT TOP 6 SAL FROM EMPLOY ORDER BY SAL DESC) EMPLOY ORDER BY SAL
SELECT * FROM
( SELECT DISTINCT TOP 6 SAL , DENSE_RANK() OVER(ORDER BY SAL DESC ) AS RANKS FROM EMPLOY)
EMPLOY WHERE RANKS=6
select all the Employees who does not have phone?
SELECT * FROM EMPLOY WHERE EMPID NOT IN(SELECT EMPID FROM PHONE)
select all the Employees who does not have phone?
select * from employees where empid not in (select empid
from phone)
2)Dispaly all managers from table(Manager id is same as
Empid)
select * from employees emp where emp.employeeid = emp.mgrid
3)How to know How many tables contain Empno as a column in
database?
4)Find duplicate rows in a table or if we have table with
one column which
has many records which are not distinct. How to find out
the distinct
values from that column and number of times it's repeated?
select salary,count(salary) as Repeat from employees group
by salary having salary > 1
5) How to delete the rows which are duplicate?(Don't remove
both duplicate
records.)
WITH [T ORDERED BY ROWID] AS
(SELECT ROW_NUMBER() OVER (ORDER BY product_name ASC) AS
ROWID, * FROM product where product_name ='Scale')
DELETE FROM [T ORDERED BY ROWID] WHERE ROWID <> 1
6)How to find the 6th highest salary?
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 6 salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary
How to know How many tables contain Empno as a column in
database?
select count(c.name) as Tables,c.name as Empno from
test.sys.tables as t inner join test.sys.columns as c on
c.object_id=t.object_id and c.name='Empno' group by c.name
having count (c.name) > 0
Namely Employee having columns like Empid,Empname,Salary,Mgrid.
Phone Table having Empid and Phone number.
Based on these some questions like this
1) select all the Employees who does not have phone?
2)Dispaly all managers from table(Manager id is same as Empid)
3)How to know How many tables contain Empno as a column in database?
4)Find duplicate rows in a table or if we have table with one column which
has many records which are not distinct. How to find out the distinct
values from that column and number of times it's repeated?
5) How to delete the rows which are duplicate?(Don't remove both duplicate
records.)
6)How to find the 6th highest salary?
Select MIN(s.salary) FROM
(SELECT TOP 6 salary FROM Employee ORDER BY salary DESC)s
How to know How many tables contain Empno as a column in
database?
SELECT DISTINCT NAME FROM SYSOBJECTS WHERE ID IN (SELECT ID FROM SYSCOLUMNS WHERE NAME = 'EMPNO')
SELECT COUNT(DISTINCT NAME)COUNT FROM SYSOBJECTS WHERE ID IN (SELECT ID FROM SYSCOLUMNS WHERE NAME = 'DEPOSITNO') AND NAME NOT LIKE 'SYNCOBJ%'
1)select all the Employees who does not have phone?
SELECT * FROM EMPLOY WHERE EMPID NOT IN(SELECT EMPID FROM PHONE)
--2)Dispaly all managers from table(Manager id is same as Empid)
SELECT * FROM EMPLOY E WHERE E.EMPID=E.MGRID
--3)How to know How many tables contain Empno as a column in database?
SELECT name FROM sys.objects WHERE OBJECT_ID IN (SELECT object_id FROM sys.columns WHERE NAME like 'EMPNO')
--4)Find duplicate rows in a table or
-- if we have table with one column which has many records which are not distinct. How to find out the distinct values from that column and number of times its repeated
SELECT * FROM (
SELECT *,ROW_NUMBER()OVER(PARTITION BY EMPNAME ORDER BY EMPID ) AS NUMS FROM EMPLOY ) A WHERE NUMS>1
CREATE TABLE ONE(ID INT)
INSERT INTO ONE VALUES('1'),('2'),('3'),('4'),('5'),('1'),('2'),('3'),('4'),('5'),('1'),('2'),('3'),('4'),('5')
SELECT ID,COUNT(*) FROM ONE group by ID having COUNT(*)>1
--5) How to delete the rows which are duplicate?(Don't remove both duplicate records.)
WITH TA
AS
(
SELECT EMPID,EMPNAME,ROW_NUMBER() OVER(PARTITION BY EMPNAME ORDER BY SAL DESC ) AS Nums
FROM EMPLOY
)
DELETE FROM TA WHERE Nums>1
WITH AT
AS
(
SELECT *,ROW_NUMBER() OVER(PARTITION BY PHONENUMBER ORDER BY EMPID DESC)AS PHONENUMS FROM PHONE
)
DELETE FROM AT WHERE PHONENUMS>1
--6)how to Find 6th highest sal ?
SELECT MIN(SAL) FROM EMPLOY WHERE SAL IN(
SELECT DISTINCT TOP(6) SAL FROM EMPLOY ORDER BY SAL DESC
)
SELECT TOP 1 SAL FROM (SELECT DISTINCT TOP 6 SAL FROM EMPLOY ORDER BY SAL DESC) EMPLOY ORDER BY SAL
SELECT * FROM
( SELECT DISTINCT TOP 6 SAL , DENSE_RANK() OVER(ORDER BY SAL DESC ) AS RANKS FROM EMPLOY)
EMPLOY WHERE RANKS=6
select all the Employees who does not have phone?
SELECT * FROM EMPLOY WHERE EMPID NOT IN(SELECT EMPID FROM PHONE)
select all the Employees who does not have phone?
select * from employees where empid not in (select empid
from phone)
2)Dispaly all managers from table(Manager id is same as
Empid)
select * from employees emp where emp.employeeid = emp.mgrid
3)How to know How many tables contain Empno as a column in
database?
4)Find duplicate rows in a table or if we have table with
one column which
has many records which are not distinct. How to find out
the distinct
values from that column and number of times it's repeated?
select salary,count(salary) as Repeat from employees group
by salary having salary > 1
5) How to delete the rows which are duplicate?(Don't remove
both duplicate
records.)
WITH [T ORDERED BY ROWID] AS
(SELECT ROW_NUMBER() OVER (ORDER BY product_name ASC) AS
ROWID, * FROM product where product_name ='Scale')
DELETE FROM [T ORDERED BY ROWID] WHERE ROWID <> 1
6)How to find the 6th highest salary?
SELECT TOP 1 salary
FROM (
SELECT DISTINCT TOP 6 salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary
How to know How many tables contain Empno as a column in
database?
select count(c.name) as Tables,c.name as Empno from
test.sys.tables as t inner join test.sys.columns as c on
c.object_id=t.object_id and c.name='Empno' group by c.name
having count (c.name) > 0