Professional Documents
Culture Documents
SQL Interview Questions and Answers: Q. What Is SQL?
SQL Interview Questions and Answers: Q. What Is SQL?
Answers
Q. What is SQL?
Using SQL we can Access Oracle, Sybase, DB2, SQL Server, MySQL, DB/400 and
other Database Management Systems
Q. When SQL was appeared?
When we are executing an SQL command for any RDBMS, the system determines
the best way to carry out our request and SQL engine figures out how to interpret
the task.
There are various components included in the process. These components are Query
Dispatcher, Optimization engines, Classic Query Engine and SQL query engine etc.
Classic query engine handles all non-SQL queries but SQL query engine won't handle
logical files.
Q. Is SQL supports Programming?
No, SQL doesn’t have Conditional and Loop statements, using SQL Commands we
can access databases.
Q. What are the sub sets of SQL?
• Data Manipulation Language
• Data Definition Language
• Data Control Language
Q. What is Data Manipulation Language?
Data Definition Language (DDL) allows us to create, alter, and delete database
objects such as schemas, tables, views, sequences, catalogs, indexes, and aliases.
Q. What is Data Control Language?
Data Control Language (DCL) allows us to control access to the database. 'DCL'
commands include-
'GRANT' to allow specific users to perform specified tasks
'REVOKE' to cancel previously denied or granted permissions
Q. What is MS Access?
Q. What is DB2?
DB2 is the short name used for DATABASE 2. It is relational database product
developed by IBM. in 1983
Q. What is DB/400?
• Arithmetic operators
• Comparison operators
• Logical operators
Operator Description
+ (Addition ) Adds values
- (Subtraction) Subtracts Right side value from Left side value
* (Multiplication) Multiplies values on either side of the operator
/ (Division) Divides left hand operand by right hand operand
% (Modulus) Divides left hand operand by right hand operand and
returns remainder
For example x = 1, y= 2
Operator Example
= (x = y) is False
!= (x != y) is True.
<> (x <> y) is true.
> (x > y) is False
< (x < y) is True
>= (x >= y) is False
<= (x <= y) is True
!< (x !< y) is False
!> (x !> y) is True.
Operator Description
-------- -----------
NOT Returns TRUE if the following condition is FALSE. Returns FALSE if it is
TRUE.
AND Returns TRUE if both component conditions are TRUE. Returns FALSE if
either is FALSE
OR Returns TRUE if either component condition is TRUE. Returns FALSE if
both are FALSE.
Database Relationship is the connection between the tables in a database. There are
4 types of relationships, and they are:
The SQL CREATE DATABASE statement is used to create new SQL database.
Syntax:
Example:
Syntax:
Example:
Syntax:
USE DatabaseName;
Example:
Syntax:
Syntax:
Syntax:
Or
Most of the Databases Management Systems like Oracle, Sybase, MS SQL Server,
MySQL, IBM-DB2 etc... using SQL for communication databases.
= Equal
When this clause is used with the DROP command, a parent table can be dropped
even when a child table exists.
Variable names should start with an alphabet. Here the table name starts with an '&'
symbol.
USER_TAB_PRIVS_MADE, USER_TAB_PRIVS_RECD
10) Which system table contains information on constraints on all the tables
created?obtained?
USER_CONSTRAINTS.
11) State true or false. EXISTS, SOME, ANY are operators in SQL?
True.
This displays the total salary of all employees. The null values in the commission
column will be replaced by 0 and added to salary.
13) What is the advantage of specifying WITH GRANT OPTION in the GRANT
command?
The privilege receiver can further grant the privileges he/she has obtained from the
owner to any other user.
START or @.
15) Which command displays the SQL command in the SQL buffer, and then
executes it?
RUN.
16) What command is used to get back the privileges offered by the GRANT
command?
REVOKE.
17) Which date function is used to find the difference between two dates?
MONTHS_BETWEEN.
LIKE operator.
19) What is the use of the DROP option in the ALTER TABLE command?
IS NULL operator.
21) What are the privileges that can be granted on a table by a user to
others?
22) Which function is used to find the largest integer less than or equal to a
specific value?
FLOOR.
DESC has two purposes. It is used to describe a schema as well as to retrieve rows
from table in descending order.
Explanation :
The query SELECT * FROM EMP ORDER BY ENAME DESC will display the output
sorted on ENAME in descending order.
Explanation:
To copy only the structure, the WHERE clause of the SELECT command should
contain a FALSE statement as in the following.
If the WHERE condition is true, then all the rows or rows satisfying the condition will
be copied to the new table.
1200.
SQL Injection is when form data contains an SQL escape sequence and injects a new
SQL query to be run.
TRUNCATE commits after deleting entire table i.e., cannot be rolled back. Database
triggers do not fire on TRUNCATE
DELETE allows the filtered deletion. Deleted records can be rolled back or committed.
Database triggers fire on DELETE.
Join is a query, which retrieves related columns or rows from multiple tables.
Equi Join - Joining two tables by equating two common columns. Non-Equi Join -
Joining two tables by equating two common columns.
Outer Join - Joining two tables in such a way that query can also retrieve rows that
do not have corresponding join value in the other table.
Sub-query is a query whose return values are used in filtering conditions of the main
query.
Retrieves rows in hierarchical order eg. select empno, ename from emp where.
INSTR returns the position of the m-th occurrence of the string 2 in string1. The
search begins from nth position of string1.
SUBSTR (String1 n, m)
SUBSTR returns a character string of size m in string1, starting from n-th position of
string1.
MINUS - returns all distinct rows selected by the first query but not by the second.
UNION ALL - returns all rows selected by either query,including all duplicates.
Maintaining data integrity through a set of rules that restrict the values of one or
more columns of the tables based on the values of primary key or unique key of the
referenced table.
SQL Queries
Create the following Tables:
LOCATION
Location_ID Regional_Group
122 NEW YORK
123 DALLAS
124 CHICAGO
167 BOSTON
DEPARTMENT
Department_ID Name Location_ID
10 ACCOUNTING 122
20 RESEARCH 124
30 SALES 123
40 OPERATIONS 167
JOB
Job_ID Function
667 CLERK
668 STAFF
669 ANALYST
670 SALESPERSON
671 MANAGER
672 PRESIDENT
EMPLOYEE
EMPLOYEE_ID LAST_NAME FIRST_NAME MIDDLE_NAME JOB_ID MANAGER_ID HIREDATE SAL
Simple Queries:
1. List all the employee details
2. List all the department details
3. List all job details
4. List all the locations
5. List out first name,last name,salary, commission for all employees
6. List out employee_id,last name,department id for all employees and rename
employee id as “ID of the employee”, last name as “Name of the employee”,
department id as “department ID”
7. List out the employees anuual salary with their names only.
Where Conditions:
Order By Clause:
18. List out the employee id, last name in ascending order based on the employee
id.
19. List out the employee id, name in descending order based on salary column
20. list out the employee details according to their last_name in ascending order
and salaries in descending order
21. list out the employee details according to their last_name in ascending order
and then on department_id in descending order.
22. How many employees who are working in different departments wise in the
organization
23. List out the department wise maximum salary, minimum salary, average
salary of the employees
24. List out the job wise maximum salary, minimum salary, average salaries of
the employees.
25. List out the no.of employees joined in every month in ascending order.
26. List out the no.of employees for each month and year, in the ascending order
based on the year, month.
27. List out the department id having atleast four employees.
28. How many employees in January month.
29. How many employees who are joined in January or September month.
30. How many employees who are joined in 1985.
31. How many employees joined each month in 1985.
32. How many employees who are joined in March 1985.
33. Which is the department id, having greater than or equal to 3 employees
joined in April 1985.
Sub-Queries
43. List out the employees who earn more than every employee in department
30.
44. List out the employees who earn more than the lowest salary in department
30.
45. Find out whose department has not employees.
46. Find out which department does not have any employees.
47.Find out the employees who earn greater than the average salary for their
department.
JOINS
Simple join
Self Join:
Outer Join:
Set Operators: