C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.
sql 1
SQL ASSIGNMENTS INTERVIEW PURPOSE
create database
sqlinterviewque go
CREATE TABLE COMPUTER
( COMPID int PRIMARY KEY, BRAND VARCHAR(50), COMPMODEL
VARCHAR(50), MANUFACTUREDATE DATE);
g
o
G
O
CREATE TABLE EMPLOYEE
( EMPID int PRIMARY KEY, FIRSTNAME VARCHAR(50),
LASTNAME VARCHAR(50), SALARY int, EMAILID
VARCHAR(50), MANAGERID int, DATEOFJOINING DATE,
DEPT VARCHAR (50), COMPID int ,
CONSTRAINT FK_COMPID FOREIGN KEY (COMPID) REFERENCES
COMPUTER(COMPID));
GO
INSERT INTO COMPUTER
VALUES(1001,'Lenovo','T480','12-JUN-19');
INSERT INTO COMPUTER
VALUES(1002,'Lenovo','T490','24-AUG-20');
INSERT INTO COMPUTER
VALUES(1003,'SONY','SQ112','01-DEC-19');
INSERT INTO COMPUTER
VALUES(1004,'SONY','SX1001','21-DEC-20');
GO
INSERT INTO EMPLOYEE
values(1,'nanda','kumar',50000,'
[email protected]',NULL,'15-JUN-
12', 'IT',1001);
INSERT INTO EMPLOYEE
values(2,'biplab','parida',30000,'
[email protected]',1,'21-DEC-
15', 'IT',1001);
INSERT INTO EMPLOYEE
values(3,'disha','patel',50000,'
[email protected]',NULL,'21-AUG-
13', 'HR',NULL);
INSERT INTO EMPLOYEE
values(4,'siba','prasad',90000,'
[email protected]',3,'01-JUN-
20','HR', 1002);
INSERT INTO EMPLOYEE
values(5,'anushka','sharma',20000,'
[email protected]',1,'01-MAR-21'
,'IT',NULL);
INSERT INTO EMPLOYEE
values(6,'somnath','maharana',65000,'
[email protected]',3,'07-MAY-19'
,'IT',1003);
GO
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 2
CREATE TABLE DUPLICATE
( EMPID int PRIMARY KEY, FIRSTNAME
VARCHAR(50), LASTNAME
VARCHAR(50), SALARY int,
EMAILID VARCHAR(50), MANAGERID int, DATEOFJOINING DATE );
GO
INSERT INTO DUPLICATE
values(1,'nanda','kumar',50000,'
[email protected]',NULL,'15-JUN-
12'); INSERT INTO DUPLICATE
values(2,'biplab','parida',30000,'
[email protected]',1,'21-DEC-
15'); INSERT INTO DUPLICATE
values(4,'siba','prasad',90000,'
[email protected]',3,'01-JUN-20');
INSERT INTO DUPLICATE
values(5,'anushka','sharma',20000,'
[email protected]',1,'01-MAR-
21')
;
INSERT INTO DUPLICATE
values(6,'biplab','parida',30000,'
[email protected]',1,'21-DEC-
15'); GO
select * from
computer; select *
from employee;
select * from duplicate;
--30 SQL QUERIES QUESTION ANSWERS FOR INTERVIEW
SQL Query EMPLOYEE to update DateOfJoining
to 15-jul-2012 for empid =1.
update EMPLOYEE
set DATEOFJOINING='15-jul-2012'
where EMPID=1
SQL Query to select all student name
where age is greater than 22.
select name,age,(*) from student
where age>22
SQL Query employee to Find all employee with
Salary between 40000 and 80000.
select * from EMPLOYEE
where SALARY between 40000 and 80000
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 3
SQL Query employee to display full name.
select firstname +''+lastname as fullname from
EMPLOYEE select from EMPLOYEE
SQL Query employee to find name of employee beginning
with S.
select * from employee
where FIRSTNAME like
's%'
Write a query to fetch details of employees whose
firstname ends with an alphabet 'A' and contains
exactly five alphabets.
select * from EMPLOYEE where FIRSTNAME like' a'
Write a query to fetch details of all
employees excluding the employees with first
names, "ANUSHKA" and "SOMNATH" from the
Employee table.
select * from EMPLOYEE
where FIRSTNAME not in ('anushka' ,'somnath')
select * from EMPLOYEE
where FIRSTNAME != 'anushka' and firstname!='somnath'
select * from EMPLOYEE
where FIRSTNAME <> 'anushka' and firstname <>'somnath'
SQL query to display the current date?
--- getting current systtem
date
select CURRENT_TIMESTAMP
select SYSDATETIME ()
,CURRENT_TIMESTAMP,GETDATE(),SYSDATETIMEOFFSET(),GETUTCDATE()
--- geting current system date
select CONVERT (date,
SYSDATETIMe()), CONVERT (date,
SYSDATETIMEOFFSET()),
CONVERT (date,
SYSUTCDATETIME()), CONVERT
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 4
(date, CURRENT_TIMESTAMP),
CONVERT (date, GETDATE()),
CONVERT (date,
GETUTCDATE())
--- getting current system
time
select CONVERT (time,
SYSDATETIMe()), CONVERT (time,
SYSDATETIMEOFFSET()),
CONVERT (time,
SYSUTCDATETIME()), CONVERT
(time, CURRENT_TIMESTAMP),
CONVERT (time, GETDATE()),
CONVERT (time ,
GETUTCDATE())
SQL Query to get day of last day of the previous month?
--- GET LAST DAY OF PREVIOUS MONTH
SELECT DATEADD (S,-1,DATEADD(MM,DATEDIFF(M,0,GETDATE()),0))AS
'LAST_DAY_OF_LASTMONTH'
---GET LAST DAY OF THIS MONTH
SELECT DATEADD(S,-1,DATEADD(MM,DATEDIFF(M,0,GETDATE())+1,0))AS 'LAST_DAY_OF
THIS MONTH'
----GET LAST DAY OF NEXT MONTH
SELECT DATEADD(S,-1,DATEADD(MM,DATEDIFF(M,0,GETDATE())+2,0))AS 'LAST DAY OF
NEXT MONTH '
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 5
Write an SQL query to fetch the employee FIRST
names and replace the A with '@'.
select REPLACE(firstname,'A','@')from EMPLOYEE
Write an SQL query to fetch the domain
from an email address SELECT * FROM
EMPLOYEE
---- GET DOMAIN NAME
SELECT SUBSTRING(EMAILID,CHARINDEX('@',EMAILID)+1,LEN(EMAILID))AS DOMAIN FROM
EMPLOYEE
--COUNT NO.OF RECORDS FOR EXTRACT DOMAIN NAME FROM EMAIL OR NO
OF EXTRACTED DOMAIN FROM SQL SERVER
SELECT SUBSTRING(EMAILID ,CHARINDEX('@',EMAILID)+1,LEN(EMAILID))AS DOMAIN
,COUNT (*)AS TOTAL_NUMBER_OF_DOMAIN
FROM EMPLOYEE
WHERE LEN(EMAILID)>1
GROUP BY SUBSTRING(EMAILID ,CHARINDEX('@',EMAILID)+1,LEN(EMAILID))
Write an SQL query to update the employee names by
removing leading and trailing spaces.
UPDATE EMPLOYEE
SET FIRSTNAME=RTRIM(LTRIM(FIRSTNAME))
Write an SQL query to fetch all the Employees details
from Employee table who joined in the Year 2020
SELECT * FROM EMPLOYEE E
WHERE E.DATEOFJOINING BETWEEN '1-JAN-2020'AND'1-DEC-2020'
SELECT *,YEAR (DATEOFJOINING)AS YEAR,MONTH(DATEOFJOINING)AS MONTH FROM EMPLOYEE
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 6
Write an SQL query to fetch only odd rows / Even rows
from the table.
---EVEN ROWS
SELECT * FROM
EMPLOYEE WHERE EMPID %
2=0
---ODD ROWS
SELECT * FROM
EMPLOYEE WHERE EMPID
%2=1
Write an SQL query to create a new table with data and
structure copied from another table.
--- CLONE TABLE WITHOUT DATA
SELECT TOP 0 * INTO EMP FROM EMPLOYEE
---INSERT DATA ---
INSERT INTO
EMP(EMPID,FIRSTNAME,LASTNAME,SALARY,DEPT,EMAILID,DATEOFJOINING,COMPID,
MANAGERID)
SELECT
EMPID,FIRSTNAME,LASTNAME,SALARY,DEPT,EMAILID,DATEOFJOINING,COMPID,MANAGERID
FROM EMPLOYEE
---CLONETABLE WITH DATA SET FROM
SAME DTABASE
SELECT * INTO EMP1 FROM EMPLOYEE
SELECT * FROM EMP1
SELECT * INTO EMP3 FROM
EMPLOYEE WHERE 1 =1
SELECT * FROM EMP3
----CLONE TABLE FROM DIFFRENT DATABASE
SELECT *INTO EMP4 FROM
asignment2_sql.dbo.EmployeeMaster1 SELECT * FROM
EMP4
--- CLONE TABLE WITH WITHOUT DATA FROM
SAME DATABASE
SELECT * INTO EMP2 FROM EMPLOYEE
WHERE 1=0
SELECT * FROM EMP2
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 7
----CLONE TABLE WITH WITHOUT DATA FROM DIFFRENT DATABASE
SELECT *INTO EMP5 FROM
asignment2_sql.dbo.EmployeeMaster1 WHERE 1=0
SELECT * FROM EMP4
Write an SQL query to create an empty table with the
same structure as some other table.
--- CLONE TABLE WITH
WITHOUT DATA
SELECT * INTO EMP2 FROM
EMPLOYEE WHERE 1=0
SELECT * FROM EMP2
----CLONE TABLE WITH WITHOUT DATA FROM DIFFRENT DATABASE
SELECT *INTO EMP5 FROM
asignment2_sql.dbo.EmployeeMaster1 WHERE 1=0
SELECT * FROM EMP4
Write an SQL query to fetch top
3 HIGHEST salaries?
select distinct top 3 SALARY from EMPLOYEE
order by( SALARY) desc
---- find N th highest salary by
order by function
SELECT top 1 * from EMPLOYEE e1
where 1-1=(select COUNT (distinct SALARY)from
EMPLOYEE e2 where e2.SALARY> e1.SALARY)
Find the first employee and last employee
from employee table?
select * from
select top 1* from EMPLOYEE order by EMPID desc
--- find first
employee
select * from
EMPLOYEE
where EMPID=(select MIN (EMPID)from
EMPLOYEE) select top 1* from EMPLOYEE
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 8
----- find last
employee
select * from
EMPLOYEE
where EMPID=(select Max(EMPID)from
EMPLOYEE) select top 1* from EMPLOYEE
order by EMPID desc
List the ways to get the count of
records in a table?
select count (*) from EMPLOYEE
Write a query to fetch the department-wise count of
employees sorted by departments count in ascending
order?
select DEPT,count(*) from
EMPLOYEE group by DEPT
order by COUNT (*)desc
Write a query to retrieve Departments who have less
than 4 employees working in it
. select DEPT,count(*)from EMPLOYEE
group by DEPT
having COUNT (EMPID)<4
Write a query to retrieve Department
wise Maximum salary.
select DEPT,MAX(SALARY)from EMPLOYEE
group by DEPT
Write a query to Employee earning maximum salary
in his department.
select * from EMPLOYEE e
where e.SALARY in (select MAX (SALARY)from
EMPLOYEE group by DEPT)
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 9
Write an SQL query to fetch the first 50% records from a table
--- by top percent
select top 50 percent * from
EMPLOYEE where DEPT= 'hr'
--- select top 3(value)* from tablename where condition
--or select top 50 percent * from EMPLOYEE where DEPT= 'hr'
Query to fetch details of employees not
having computer
select * from EMPLOYEE
where COMPID is null
---- Query to fetch details of employees
having computer
select * from EMPLOYEE
where COMPID is not null
--- find employee who is not manager
select *from EMPLOYEE where MANAGERID is null
--- find employee who is manager
select *from EMPLOYEE where MANAGERID is not null
Query to fetch employee details along with the computer
details who have been assigned with a computer.
select e.empid,e.firstname,e.lastname,e.salary,e.emailid,e.MANAGERID,
e.dept,e.DATEOFJOINING,c.BRAND,c.COMPMODEL,c.MANUFACTUREDATE from EMPLOYEE e
inner join COMPUTER c on e.COMPID=c.COMPID
----compid column not exist
select * from EMPLOYEE e join COMPUTER c on E.COMPID=c.COMPID
Fetch all employee details along with the computer name
assigned to them.
select * ,case when c.BRAND is null then 'not asigned' else 'asigned
'end from
EMPLOYEE e
left join COMPUTER c on e.COMPID=c.COMPID
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 10
----compid column not exist
select * from EMPLOYEE e left join COMPUTER c on E.COMPID=c.COMPID
Fetch all Computer Details along with employee
name using it
select * from COMPUTER c left join EMPLOYEE e on
e.COMPID=c.COMPID
Delete duplicate records from a table
;with cte
as
(select *,ROW_NUMBER ()over (partition by SALARY order by empid )AS Dup from
DUPLICATE ) delete FROM cte
where Dup>1
select* from DUPLICATE
Find Nth Highest salary
--- find nth highest salary
select distinct SALARY from employee e1
where 2=(select count (distinct e2.SALARY) from
EMPLOYEE e2 where e2.SALARY > e1.SALARY)
select COUNT (distinct SALARY) from
EMPLOYEE select * from EMPLOYEE
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 11
-----rank function including
duplicate salary
select distinct * from
(select *,RANK()over
(order by salary desc)as R from
EMPLOYEE)as k where R =1;
---- find nth highest excluding
duplicate salary
select distinct *from
(select distinct
salary,dense_rank()over (order by salary
desc)as R from EMPLOYEE)as k where R
=3;
--- dense rank function excluding
duplicate salary
select distinct *from
(select distinct salary,rank()over
(order by salary desc)as R from EMPLOYEE)as k
where R =3;
----- find last identity
/*identity column
syntax : column name data type identity (seed,increment) */
create table test1
(id int identity
(1,2), name
varchar (50))
insert
into test1
values
('vijay')
insert
into test1
values
('vijay')
insert
into test1
values
('vijay')
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 12
insert
into test1
values
('vijay')
insert
into test1
values
('vijay')
insert
into test1
values
('vijay')
--- select
select * from test1
-----
delete
delete
from test1
----
truncate
truncate
table test1
----when use delete comand identity valuew not ressed
--- when use truncate command identity
value is reseed
delete from test1
where id =11
----- insert deleted value by identity insert on
---when identity insert on compulsery give identity column
value rather than give other column value
identity insert test1 on
identity insert test1 off
----- identity ressed for start
from new identity
DBCC CHECKIDENT('TEST1',RESEED,0)
C:\Users\Dell\Documents\SQL Server Management Studio\Projects\SQL INTERVIEW QUESTIONS.sql 13
----select SCOPE_IDENTITY() use for same session or same scope
identity column value
---select @@IDENTITY use for same session or any scope identity
column value first make trigger
---select ident_current('tablename') use for any session or any
scope