SQL Practice Sheet For Interview
SQL Practice Sheet For Interview
First_nam
Employee_id Last_name Salary Joining_date Department
e
100000
1 John Abraham 01-JAN-13 12.00.00 AM Banking
0
TestName
7 123 650000 01-JAN-13 12.00.00 AM Services
1
TestName
8 Lname% 600000 01-FEB-13 12.00.00 AM Insurance
2
1 01-FEB-13 5000
2 01-FEB-13 3000
3 01-FEB-13 4000
1 01-JAN-13 4500
2 01-JAN-13 3500
MySQL Server Equivalent of Oracle SUBSTR is SUBSTRING. In MySQL start position is 1, Query : select
substring(FIRST_NAME,1,3) from employee
MySQL Server Equivalent of Oracle INSTR is LOCATE, Query: Select LOCATE('o',FIRST_NAME) from
employee where first_name='John'
SQL Server Equivalent of MySQL concat is '+', Query : Select FIRST_NAME + '_' +LAST_NAME from
EMPLOYEE
15. Get all employee details from the employee table order
by First_Name Ascending
Select * from employee order by FIRST_NAME asc
16. Get all employee details from the employee table order
by First_Name descending
Select * from employee order by FIRST_NAME desc
17. Get all employee details from the employee table order
by First_Name Ascending and Salary descending
Select * from employee order by FIRST_NAME asc,SALARY desc
SQL Queries in SQL Server (Format - “MM/DD/YYYY”), Select * from EMPLOYEE where joining_date
<'01/01/2013'
SQL Queries in MySQL (Format - “YYYY-DD-MM”), Select * from EMPLOYEE where joining_date <'2013-
01-01'
SQL Queries in SQL Server and MySQL (Format - “MM/DD/YYYY”), Select * from EMPLOYEE where
joining_date >'01/31/2013'
SQL Queries in MySQL (Format - “YYYY-DD-MM”), Select * from EMPLOYEE where joining_date >'2013-
01-31'
SQL Queries in SQL Server, Select FIRST_NAME from employee where Last_Name like '%[%]%'
SQL Queries in MySQL, Select FIRST_NAME from employee where Last_Name like '%\%%'
SQL Queries in SQL Server and MySQL, Select REPLACE(LAST_NAME,'%',' ') from employee
55. Insert into employee table Last Name with " ' " (Single
Quote - Special Character)
Tip - Use another single quote before special character
Insert into employee (LAST_NAME) values ('Test''')
56. Select Last Name from employee table which contain
only numbers
Select * from EMPLOYEE where lower(LAST_NAME)=upper(LAST_NAME)
59. Select first_name, incentive amount from employee and incentives table for those
employees who have incentives
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
60. Select first_name, incentive amount from employee and incentives table for those
employees who have incentives and incentive amount greater than 3000
Select FIRST_NAME,INCENTIVE_AMOUNT from employee a inner join incentives B on
61. Select first_name, incentive amount from employee and incentives table for all employes
even if they didn't get incentives
A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
62. Select first_name, incentive amount from employee and incentives table for all employees
even if they didn't get incentives and set incentive amount as 0 for those employees who
didn't get incentives.
SQL Queries in Oracle, Select FIRST_NAME,nvl(INCENTIVE_AMOUNT,0) from employee a left join incentives
B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
SQL Queries in SQL Server, Select FIRST_NAME, ISNULL(INCENTIVE_AMOUNT,0) from employee a left join
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
SQL Queries in MySQL, Select FIRST_NAME, IFNULL(INCENTIVE_AMOUNT,0) from employee a left join
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
63. Select first_name, incentive amount from employee and incentives table for all employees
who got incentives using left join
SQL Queries in Oracle, Select FIRST_NAME,nvl(INCENTIVE_AMOUNT,0) from employee a right join
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
SQL Queries in SQL Server, Select FIRST_NAME, isnull(INCENTIVE_AMOUNT,0) from employee a right join
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
SQL Queries in MySQL, Select FIRST_NAME, IFNULL(INCENTIVE_AMOUNT,0) from employee a right join
incentives B on A.EMPLOYEE_ID=B.EMPLOYEE_REF_ID
64. Select max incentive with respect to employee from employee and incentives table using
sub query
SQL Queries in Oracle, select * from (select * from employee order by SALARY desc) where rownum <3
SQL Queries in SQL Server, select top 2 * from employee order by salary desc
SQL Queries in MySQL, select * from employee order by salary desc limit 2
SQL Queries in Oracle, select * from (select * from employee order by SALARY desc) where rownum <N + 1
SQL Queries in Oracle, select min(salary) from (select * from (select * from employee order by SALARY desc)
SQL Queries in SQL Server, select min(SALARY) from (select top 2 * from employee) a
SQL Queries in MySQL, select min(SALARY) from (select * from employee order by salary desc limit 2) a
SQL Queries in Oracle, select min(salary) from (select * from (select * from employee order by SALARY desc)
SQL Queries in SQL Server, select min(SALARY) from (select top N * from employee) a
SQL Queries in MySQL, select min(SALARY) from (select * from employee order by salary desc limit N) a
Both UNION and UNION ALL is used to select information from structurally similar tables. That means
corresponding columns specified in the union should have same data type. For example, in the above query, if
FIRST_NAME is DOUBLE and LAST_NAME is STRING above query wont work. Since the data type of both the
columns are VARCHAR, union is made possible. Difference between UNION and UNION ALL is that , UNION
EMPLOYEE_ID NUMBER,
FIRST_NAME VARCHAR2(20 BYTE),
SALARY FLOAT(126),
KEY(EMPLOYEE_ID,FIRST_NAME)
76. Write Sql Syntax to create EMPLOYEE_REF_ID in INCENTIVES table as foreign key with
respect to EMPLOYEE_ID in employee table
REFERENCES EMPLOYEE(EMPLOYEE_ID)
NOCACHE NOORDER;
79. Write Sql syntax to create Oracle Trigger before insert of each row in employee table
DECLARE
seq_no number(12);
BEGIN
END;
SHOW ERRORS;
REFRESH COMPLETE
NEXT SYSDATE + 1 AS
where a.EMPLOYEE_ID=b.EMPLOYEE_REF_ID
BUILD IMMEDIATE
SQL Injection is one of the the techniques uses by hackers to hack a website by injecting SQL commands in
data fields.