0% found this document useful (0 votes)
26 views

SQL Questions.

This document contains 41 SQL practice questions and answers related to various SQL clauses and operators. It covers basic queries to retrieve data from tables, filtering results using WHERE, ORDER BY, aggregate functions like COUNT, SUM, AVG, MAX, GROUP BY, HAVING, and subqueries. The questions progress from simple selects to more complex queries using joins, nested queries, and multiple clauses.

Uploaded by

ranayadav328
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

SQL Questions.

This document contains 41 SQL practice questions and answers related to various SQL clauses and operators. It covers basic queries to retrieve data from tables, filtering results using WHERE, ORDER BY, aggregate functions like COUNT, SUM, AVG, MAX, GROUP BY, HAVING, and subqueries. The questions progress from simple selects to more complex queries using joins, nested queries, and multiple clauses.

Uploaded by

ranayadav328
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

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):


Single row subquery
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';

Multi row subquery:


43. WAQ to display all records of those employees who salary is equal to all
IT_PROG salary.
Select * from employee where salary in (Select salary form employees where
job_id=’IT_PROG’)
44. WAQ to display all records of those employees whose salary equal to all
department maximum salary.
Select * from employee where salary in(select max(salary) from employees
group by department_id)

45. WAQ to display all records of those employee whose department_name is IT.

Select * from employees where Department_id in ( select Department_id from


department where Department_name = 'IT');

46. WAQ to display all records of those employees whose department_name is


sales and executive and getting salary between 10000 to 20000.
Select * from employees where Department_id in (select Department_id from
Departments where Department_name in ('Sales’, ‘Executive')) and salary
between 10000 and 20000;
47. WAQ to display all record of those employees whose city is Seattle

select * from employees where department_id in (Select department_id from


departments where location_id in (Select location_id from locations where
city='Seattle'))

Join Statements: (Query from multiple table.)


Inner Join:
48. WAQ to display employee_id, first name, salary and department_name whose
department_name is IT and Management.
Select employee_id , First_name , Salary , department_name from employees
e inner join departments d on e.department_id=d..Department_id where
Department_name in ('IT', 'Management');

49. WAQ to display employee_id, first name, last_name, salary, department_id


and department_name of those employee who working as IT_PROG and
belongs to IT department.
Select employee_id, first_name, last_name, salary, d.department_id,
department_name from employees e inner join departments d on
e.department_id=d.department_id where job_id=’IT_PROG’ and
department_name=’IT’

Record access from three table:


Syntax:
Select */<Columns name> from (<table1> inner join <table 2> on
<table1>.<common column>=<table2>.<common column>) inner join <table 3>
on <table2>.<common column>=<table3>.<common column>
50. WAQ to display employee_id, salary, department_name, location_id, city
of those employess whos location_id=1100.

Select e.employee_id, e.first_name, e.salary, l.location_id, l.city,


d.department_name from (employees e inner join departments d on
e.department_id= d.department_id) inner join locations l on
d.location_id=l.location_id where location_id=1100;

Set Operation
Union Operation.
51.WAQ to display all employees details who are working in department_id
60 and 90.
Select * from employees where department_id=60 union Select * from
employees where department_id=90.
Create Table:
Syntax:
Create Table <Table name>(<column name 1> <data type>(size), <column
name 2> <data type>(size), <column name 3> <data type>(size),………….);

Example
Create table Student(s_id number(3), Name Varchar2(20), Fee
number(8,2))

O/P: Table is created

Description of table

Desc <Table name>

Insert Into:
Syntax:
Insert into <Table name>(<column name1>, <column name 2>, …………
)values (<value1>, <value2>, ……….)

Example:
Insert into student (s_id, name, fee) values (1, ‘Ankur’, 45000)

How to insert record at run time in table

Syntax:
Insert into student >(<column name1>, <column name 2>, ………… )
values(&(<value1>,& <value2>, ……….)

Insert record in new table from existing table.

52. Insert record into st1 table from employees table employee_id and
first_name which are belongs form department_id 50.

insert into st2 (id , name) (select employee_id , first_name from employees
where department_id =50)
Table Alteration:
Alter Table
Clause: Add, Drop, Modify, Rename

Alter Table use Add clause.


Add column in existing table.

Syntax:
Alter Table <table name> add <column name> <datatype>(size);

Delete column from existing table.


Syntax:
Alter table <table name> drop column <column name>;

Change size of existing column.


Syntax:
Alter table <table name> modify <column name> (size);

53. Write q query to add column email id in Student table.

Alter table student add email_id varchar2(20).

54. Write q query to delete email id column from Student table.

After table student drop column email_id

55. Write q query to increase size of email_id column of Student table.

Alter Table Student Modify Email_Id varchar2(30);

Change column name of existing table.


Syntax:
Alter table <Table name> rename column <old column name> to <new column
name>
Example:
Alter table student rename column mobile_no to contact_no.
Rename existing table: Rename command is used to change name of table.
Syntax:
Rename <old table name> to <new table name>

Create Synonym of any existing table:


Synonym is just alias name of table that means another name of table.
Syntax:
Create synonym <Table name synonym> for <table name>

Constraints:

• Not Null
• Unique
• Primary Key
• Check
• Foreign key (references)
• Default

Update Command: Used to update record of any existing table.


Syntax:
Update <Table name> set <column name1>=<updated column value1>,<column name
2>=<updated column value 2>, ………………….where <condition>

Ex:
Update emp set salary=salary+2000.

56. Write a query to update salary by 5% who


working in department 10 and 20
update emp set salary= salary+salary*0.05 where
dept_in in ('10' , '20')
57. WAQ to update employee post as Analyst who
previously working as Clerk.

Update employee set job_id=’Analyst’ where


job_id=’Clerk’;

58. WAQ to update employee salary by 10 % and


department id 20 who previously working in
department 10 and 30.

Update emp set salary=salary+salary*0.1,


department_id=20 where department_id in(10,30);
59. WAQ to update employees salary by 15 % and
job_id is Manager who getting maximum salary and
minimum salary.

You might also like