Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

SQL Practice Questions

1. Write a query to display all record of employee.

Ans:- Select * form emp;


2. Write a query to display id, name and salary of employee.

Ans: Select id, name, salary from emp;

3. Write a query to display employee_id, name and salary, department of all


employees also display increased salary by 2000.

Ans: Select employee_id, first_name, salary+2000, department_id from


employees;
4. Write a query to display employee_id, name and salary, department of all
employees also display increased salary by 5%.
Alias Name:-

Operators in SQL:
Arithmetic Operator
Operator Symbol Operation

+ Addition

- Subtraction

* Multiplication

/ Division

Mod Remainder
Comparison/Relational Operator
Operator Symbol Operation

> Greater than

< Smaller than

>= Greater than equal to

<= Smaller than equal to

= Equal to

<> Not equal to

Logical Operator
Operator Symbol Operation

AND And Logical Operator

OR Or Logical Operator

NOT Not logical Operator

5. WAQ to display all records of employees whose salary equal to 24000.

Select * from employees where salary=24000

6. WAQ to display employee_id, frst_name, salary of those employee whose


salary less than 15000.

Select employee_id, first_name, salary from employees where salary<15000;

7. WAQ to display employee_id, frst_name, salary of those employee whose


job_id is IT_Prog
Select employee_id, first_name, salary from employees where job_id=
‘IT_Prog’

8. WAQ to display employee_id, frst_name, salary,department_id of those


employee who working as pu_clerk and salary not equal to 20000.

Select employee_id, first_name, salary, department_id from employees where


job_id=’PU_CLERK’ and salary <>2000.

9. WAQ to display all records of those employee who job_id 10 or salary equal to
20000.
10. WAQ to display all records of those employee who salary between 10000 and
20000.

Select * from employees where Salary >=10000 and salary <= 20000;

Or

Select * from employees where salary between 10000 and 20000.

11. WAQ to display all records of those employee who salary not between 10000
and 20000 and job_id= Programmer

Select * from employees where Salary <10000 and salary > 20000 and
job_id=’Programmer’;
Or
Select * from employees where Salary not between 10000 and 20000 and
job_id=’Programmer’;

Between …… and operator:- This operator is used when condition in range.


Syntax:
Between <Lower value> and <Upper value>
Note:- In this operator lower value and upper value both are included.
12. WAQ to display all records of those employee who joining date is 20 June
1999 to 25 Aug 2001.
Select * from employees where hire_date between ‘20-JUN- 1999’ and ‘25-
AUG-2001’
13. WAQ to display all records of those employee who joining date is 7 December
1999 to 25 Aug 2001 and salary between 12000 to 17000.

select * from employees where hire_date between '07-DEC-1999' and '25-


AUG-2001' and salary between 12000 and 17000.

Not between…..and operator

14. WAQ to display all records of those employee who working in department 10,
20 and 30.
Select * from employees where department_id=10 or department_id=20 or
department_id=30.
Or
Select * from employees where department_id in (10,20,30)
15. WAQ to display employee_id, email, salary, job_id of those employee who
working as faculty and clerk and salary greater than equal to 12000.

Select employee_id, email, salary, job_id from employees where


job_id=’Clerk’ or job_id=‘Faculty’ and salary>=12000.
Or
Select employee_id, email, salary, job_id from employees where job_id in
(’Clerk’ ,‘Faculty’) and salary>=12000.

In Operator: - In operator is used when one column has multiple values.

Syntax:
<Column Name> in(value1, value2,…….)

16. WAQ to display all records of those employee who working as programmer
and analyst and department 10 and 20 and hire_date from 20-Aug-2018 to
19- AUG- 2021.
select * from employees where job_id in(‘Programmer’, ‘Analyst’) and
department_id in(10,20) and hire_date between ‘20-AUG-2018’ and ‘19-AUG-
2021’

Like Operator: It is used extract pattern based record from table. It match any
given character or set of characters form existing table record and gives result.

It provides two symbols.


_ :- It represents single character.
% :- It represents multiple characters.

Syntax:
<Column name> like ‘_/%<pattern character>_/%’

Note: Like operator used with only string column.

17. WAQ to display all records of those employee whose first_name started from
A character.
Select * from employees where first_name like ‘A%’

18. WAQ to display all records of those employee whose first_name last character
is r.

Select * from employees where first_name like ‘%r’

19. WAQ to display all records of those employee whose first_name second
character is a and last_name second last character is i.

Select * from employees where FIRST_NAME like '_a%' and LAST_NAME like
'%i_' ;

20. WAQ to display employee_id, first_name as name and salary increased by 10%
as Increased salary of those employee whose first_name third last character is
p and getting salary 10000, 20000 and 30000.
Select employee_id, first_name “Name”, Salary+Salary*10/100 “Increase
Salary” from employees where first_name like ‘%p_ _’ and salary in(10000,
20000, 30000)

Is Null: - This operator is used find those records which are empty.
Syntax: -
<Column name> is null
21. WAQ to display all records of those employee whose department id is blank.

Select * from employees where department_id is null;

22. WAQ to display all records of those employee whose first_name not started
with A letter and salary is not mentioned.

Select * from employees where First_name not like 'A%' and salary is null;
Is Not Null : It negates is null value.
Distinct:- This keyword is used to show only unique value of any column.

23. WAQ to display all unique first_name.

select distinct first_name from employees;

24. WAQ to display all type of department id.

ORDER BY Clause: -
Syntax: -
Order by <Column name>/<column position in query> ASC / DESC
Note: ASC is optional.
25. WAQ to display all records in ascending order using salary column.

Select * from employees order by salary asc


26. WAQ to display employee_id,first_name and salary in decending order using
salary column.
Select employee _Id, first_name, salary from employees order by salary desc
27. WAQ to display how many employees working in department 10 and 20.

select count(*) from employees where department_id in(10,20)

28. WAQ to display total of unique name.


Select count(distinct first_name) from employees

29. WAQ to display addition of salary.


Select sum(salary) from employees;

30. WAQ to display total amount of salary who working as programmer and
department 10 and 30.

Select sum(salary) from employees where JOB_ID ='Programmer' and


Department_id in (10, 30);

31. WAQ to display average salary and total salary of those employee who working
as Programmer and hire_date from 01-01-20218 to 02-01-2020.

Select sum(salary),avg(salary) from employees where JOB_ID ='Programmer'


and HIRE_DATE between '01-Jan-18' and '02-Jan-20' ;

32. WAQ to display maximum salary employee table.


Select max(salary) from employees

33. WAQ to display maximum salary in department 10 and 20.

Select max(salary) from employees where Department_id in (10, 20);

34. WAQ to display all records of those employee who getting maximum salary.
Select * from employees where salary=24000

Group By Clause
35. WAQ to display total salary of each department.
Select sum(salary) from employees group by department_id;

36. WAQ to display each post total number of employee and maximum salary.

Select job_id, count(*),max(salary) from employees group by job_id;

37. WAQ to display total count of each name.

select first_name, count(*) from employees group by first_name

HAVING Clause :
The SQL HAVING clause is used in combination with the GROUP BY clause to restrict
the groups of returned rows to only those whose the condition is TRUE. The
HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions column. The HAVING clause must follow the GROUP BY
clause in a query and must also precedes the ORDER BY clause if used.
Syntax:-
Having <aggregate function column> operator value;
38. WAQ to display those department which have 5 IT_PROG.

Select department_id,count(*) from employees where job_id=’IT_PROG’


group by department_id having count(*)>=5.

39. WAQ to display those job which have 5 IT_PROG.


Sub Query(Nested Query):

40. WAQ to display all record of those employee whose salary is equal to
employee_id 105’s salary.

Select * from employee where salary= (Select salary from employees where
employee_id=105);

41. WAQ to display all records of those employees who getting maximum salary.

Select * from employees where salary = (select max (salary ) from employees)
42. WAQ to display employee_id, first_name and salary of those employees who
getting minimum salary and working IT_PROG.

select employee_id, first_name, salary from employees where salary = (select


min(salary) from employees) and job_id = 'IT_PROG';

You might also like