SQL Interview Questions
SQL Interview Questions
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
1NF – Removes duplicated attributes, Attribute data should be
atomic, and attribute should be the same kind.
2NF – This should be in 1NF and each non-key is fully dependent on
the primary key.
3NF – This should be in 2NF and all the non-key attributes which are
not dependent on the primary key should be removed. All the
attributes which are dependent on the other non-key attributes
should also be removed. Normalization is done in.
3) What is denormalization and when would you go for it?
It is the reverse process of normalization. It increases query
performance by reducing the joins. It is used for OLAP applications.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
5) What is the difference between the Primary key and the Unique
key.
Primary Key
1.Enforces uniqueness of the column in a table
2.Default clustered index
3.Does do not Allow nulls
Unique Key
1. Enforces the uniqueness of the column in a table.
2.Default non-clustered index.
3.Allows one null value
6) Define the following keys:
Candidate key, Alternate key, Composite key.
1.Candidate key –Key which can uniquely identify a row in the table.
2. Alternate key –If the table has more than one candidate key and
when one becomes a primary key the rest becomes alternate keys.
3. Composite key –More than one key uniquely identifies a row in a
table.
7) What are defaults? Is there a column to which a default can’t be
bound?
It is a value that will be used by a column if no value is supplied to
that column while inserting data.
I can’t be assigned for identity and timestamp values.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
8) What are user-defined data types and when you should go for
them?
Lets you extend the base SQL Server data types by providing a
descriptive name and format to the database
E.g. Flight_num appears in many tables and all these tables have
varchar(8)
Create a user-defined data-type
10) What part does database design have to play in the performance
of a SQL Server-based application?
It plays a very major part. When building a new system, or adding to
an existing system, the design must be correct. Ensuring that the
correct data is captured and is placed in the appropriate tables, that
the right relationships exist between the tables, and that data
redundancy is eliminated is an ultimate goal when considering
performance. Planning a design should be an iterative process, and
constantly reviewed as an application is developed.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
matter how slightly. Therefore, a designer has to be on top of this
and ensure that the design of the database remains efficient.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
2.Physical – Design, Implementation, and Maintenance
Types of cursors:
Disadvantages of cursors:
Each time you fetch a row from the cursor, it results in a network
roundtrip, whereas a normal SELECT query makes only one
roundtrip, however large the result set is. Cursors are also costly
because they require more resources and temporary storage (which
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
results in more IO operations). Further, there are restrictions on the
SELECT statements that can be used with some types of cursors.
Most of the time set-based operations can be used instead of
cursors.
Here is an example:
If you have to give a flat hike to your employees using the following
criteria:
14) Write down the general syntax for a SELECT statement covering
all the options.
Here’s the basic syntax: (Also checkout SELECT in books online for
advanced syntax).
SELECT select_list
[HDEV:INTO new_table_]
FROM table_source
[HDEV:WHERE search_condition]
[HDEV:GROUP BY group_by_expression]
[HDEV:HAVING search_condition]
[ORDER BY order_expression [ASC | HDEV:DESC] ]
15) What is a Join? Explain Different Types of Joins
Joins are used in queries to explain how different tables are related.
Joins also let you select data from a table depending upon data from
another table.
Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER
JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER
JOINS, and FULL OUTER JOINS.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
Yes, very much. Check out BEGIN TRAN, COMMIT, ROLLBACK, SAVE
TRAN, and @@TRANCOUNT
18) What is the system function to get the current user’s user id?
USER_ID(). Also check out other system functions like USER_NAME(),
SYSTEM_USER, SESSION_USER, CURRENT_USER, USER, SUSER_SID(),
HOST_NAME().
19) What are triggers? How many triggers you can have on a table?
How to invoke a trigger on demand?
Triggers are special kinds of stored procedures that get executed
automatically when an INSERT, UPDATE or DELETE operation takes
place on a table. In SQL Server 6.5 you could define only 3 triggers
per table, one for INSERT, one for UPDATE, and one for DELETE. From
SQL Server 7.0 onwards, this restriction is gone, and you could create
multiple triggers per each action. But in 7.0 there’s no way to control
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
the order in which the triggers fire. In SQL Server 2000 you could
specify which trigger fires first or fires last using sp_settriggerorder.
Till SQL Server 7.0, triggers fire only after the data modification
operation happens. So in a way, they are called post triggers. But in
SQL Server 2000 you could create pre triggers also – INSTEAD OF
triggers.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
empid int,
mgrid int,
empname char(10)
)
INSERT emp SELECT 1,2,’Vyas’
INSERT emp SELECT 2,3,’Mohan’
INSERT emp SELECT 3,NULL,’Shobha’
INSERT emp SELECT 4,2,’Shridhar’
INSERT emp SELECT 5,2,’Sourabh’
SELECT t1.empname [HDEV:Employee], t2.empname
[HDEV:Manager]
FROM emp t1, emp t2
WHERE t1.mgrid = t2.empid
Here’s an advanced query using a LEFT OUTER JOIN that even returns
the employees without managers (super bosses)
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
SELECT DATENAME(dw, DATEADD(dd, – DATEPART(dd, GETDATE()) +
1, GETDATE())) AS FirstDay
22) How to find the 6th highest salary from the Employee table?
SELECT TOP 1 salary FROM (SELECT DISTINCT TOP 6 salary FROM
employee ORDER BY salary DESC) a ORDER BY salary
23) How can I enforce using of a particular index?
You can use index hint (index=index_name) after the table name.
SELECT au_lname FROM authors (index=aunmind)
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
it updates information about the distribution of key values for one or
more statistics groups (collections) in the specified table or indexed
view.
29) What do you consider are the best reasons to use stored
procedures in your application instead of passing Transact-SQL code
directly to SQL Server?
First and foremost, a stored procedure is a compiled set of code,
where passing T-SQL through languages such as VB, Visual FoxPro,
etc., means that the set of code needs to be compiled first. Although
T-SQL within VB, etc., can be prepared before running, this is still
slower than using a stored procedure. Then, of course, there is the
security aspect, where, by building a stored procedure, you can place
a great deal of security around it.
When dealing with sensitive data, you can use an encrypted stored
procedure to hide sensitive columns, calculations, and so on. Finally,
by using a stored procedure, I feel that transactional processing
becomes a great deal easier and, in fact, using nested transactions
becomes more insular and secure. Having to deal with transactions
within code that may have front-end code, will slow up a transaction,
and therefore a lock will be held for longer than necessary.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
30) What are some techniques for writing fast-performing stored
procedures?
Fast performing stored procedures are like several other areas within
T-SQL. Revisiting stored procedures every six months or so, to ensure
that they are still running at their optimum performance is essential.
However, actual techniques themselves include working with as
short a transaction area as possible, as lock contention will certainly
impact performance.
But what you may find tends to help, is to break down the code and
try to determine which join it is that is causing the performance
problem. Then analyze this specific join and see why it is a problem.
Always check out a stored procedure’s performance as you build it
up by using the SHOWPLAN commands.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
31) When should SQL Server-based cursors be used, and not be
used?
SQL Server cursors are perfect when you want to work one record at
a time, rather than taking all the data from a table as a single bulk.
However, they should be used with care as they can affect
performance, especially when the volume of data increases. From a
beginner’s viewpoint, I really do feel that cursors should be avoided
every time because if they are badly written, or deal with too much
data, they really will impact a system’s performance.
The main area of your focus should be, is there an alternative way of
doing things? Even if I have to break this down into several chunks of
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
work, can I do this work without using cursors, and so result in faster
performance. Another area that you can look at is the use of CASE
statements within your query.
By using a CASE statement, you can check the value within a column
and make decisions and operations based on what you have found.
Although you will still be working on a whole set of data, rather than
a subset found in a cursor, you can use CASE to leave values, or
records as they are, if they do not meet the right criteria. Care should
be taken here though, to make sure that by looking at all the data,
you will not be creating a large performance impact.
33) If you have no choice but to use a SQL Server-based cursor, what
tips do you have to optimize them?
Perhaps the best performance gain is when you can create a cursor
asynchronously rather than needing the whole population operation
to be completed before further processing can continue. Then, by
checking specific global variables settings, you can tell when there is
no further processing to take place. However, even here, care has to
be taken. The asynchronous population should only occur on large
record sets rather than those that only deal with a small number of
rows.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
Use the smallest set of data possible. Break out of the cursor loop as
soon as you can. If you find that a problem has occurred, or
processing has ended before the full cursor has been processed, then
exit. If you are using the same cursor more than once in a batch of
work, and this could mean within more than one stored procedure,
then define the cursor as a global cursor by using the GLOBAL
keyword, and not closing or deallocating the cursor until the whole
process is finished. A fair amount of time will be saved, as the cursor
and the data contained will already be defined, ready for you to use.
34) What are the steps you will take to improve the performance of a
poor-performing query?
This is a very open-ended question and there could be a lot of
reasons behind the poor performance of a query. But some general
issues that you could talk about would be:
No indexes
No Table scans
Missing or out of date statistics
Blocking
Excess recompilations of stored procedures
35) What is an ER Diagram?
An ER diagram or Entity-Relationship diagram is a special picture
used to represent the requirements and assumptions in a system
from a top-down perspective. It shows the relations between entities
(tables) in a database.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
42) What is Durability?
Guarantees that the database will keep track of pending changes so
that the server will be able to recover if an error occurs.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
The way in which two or more concepts/entities are connected, or
the state of being connected.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
54) What are associative entities?
An associative entity is a conceptual concept. An associative entity
can be thought of as both an entity and a relationship since it
encapsulates properties from both. It is a relationship since it is
serving to join two or more entities together, but it is also an entity
since it may have its own properties.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
59) What is a simple key?
It is a key that is composed of one attribute.
Give/ recite the types of UDF functions.
Scalar, In-line, Multi
Unique key – can have more than one per table. It can have null
values. It cannot have repeating values. Maximum of 999 clustered
indexes per table.
61) What do you mean by CTEs? How will you use it?
CTEs also is known as common table expressions are used to create a
temporary table that will only exist for the duration of a query. They
are used to create a temporary table whose content you can
reference to simplify a query structure.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
63) What would the command: DENY CREATE TABLE TO Peter do?
It wouldn’t allow the user Peter to operate CREATE TABLE regardless
of his role.
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
72) What are before images, after images, undo activities, and redo
activities in relation to transactions?
Before images refer to the changes that are rolled back on if a
transaction is rolled back. After images are used to roll forward and
enforce a transaction. Using the before images are called the undo
activity. Using after images are called the redo activity.
Explore SQL Server Sample Resumes! Download & Edit, Get Noticed
by Top Employers!
73) What are shared, exclusive, and updated locks?
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
A shared lock locks a row so that it can only be read. An exclusive
lock locks a row so that only one operation can be performed on it at
a time. An update lock basically has the ability to convert a shared
lock into an exclusive lock.
Department:
Department_No Department_Name
10 ECE
20 ECE
30 CSE
40 IT
Employee Details:
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
5 James Hyderabad24 50 230000
SQL Query Interview Questions with Answers
81) Write a Query to display employee details who are working in the
ECE department?
SELECT employee.employee_name, employee.address,
employee.salary, employee.age,
FROM Department D
INNER JOIN Employees E
ON department.D_no=employee.D_no WHERE
department.D_name= ‘ECE’
82) Write a Query to display employee details?
SELECT * FROM employee;
83) Write a Query to display employee details along with
department_name?
SELECT employee.employee_no, employee.employee_name,
employee.address, employee.salary, employee.age,
department.department_name
FROM department D
INNER JOIN employee E
ON department.D_no=employee.D_no
84) Write a Query to display employee details whose sal>20000 and
who is working in the ECE department?
SELECT employee.employee_no, employee.employee_name,
employee.address, employee.salary, employee.age
FROM department D
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
INNER JOIN employee E
ON dept.D_no=emp.D_no
WHERE dept.D_name=’ECE’ and E.salary>20000
85) Write a Query to display employee details along with
department_name and who is working in the ECE department,
whose name starts with a?
SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age,
dept.dname
FROM department D
INNER JOIN employee E
ON dept.D_no=emp.D_no
WHERE dept.D_name=’ECE’ and emp.E_name like ‘a%’
86) Write a Query to display employee details along with
department_name and whose age between 20 and 24?
SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age,
dept.d_name
FROM department D
INNER JOIN employee E
ON dept.D_no=emp.D_no
WHERE E.age between 20 and 24
87) Write a Query to display employee details along with
department_name and who are staying in Hyderabad?
SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age,
dept.d_name
FROM department D
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
INNER JOIN employee E
ON dept.D_no=emp.D_no
WHERE E.address=’hyd’
88) Write a Query to display employee details whose salary>20000
and whose age>20 & who is working in the ECE department?
SELECT emp.e_no, emp.e_name, emp.address, emp.salary, emp.age,
dept.d_name
FROM department D
INNER JOIN employee E
ON dept.D_no=emp.D_no
WHERE E.age>20 and E.salary>20000 and dept.D_name=’ECE’
State Table:
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
3 Vijayawada S2
4 Chennai S3
5 Madhurai S3
6 Bangalore S4
Blood Group Details
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
FROM State S
INNER JOIN City C
ON S.State_ID
WHERE S.State_Name ‘AP’
90) Write a Query to display Donor_ID, Donor_Name, Phone No,
City?
SELECT D.Donor_ID, D_Name, D_Phone No, C.City_Name
FROM Donor D
INNER JOIN City C
ON D.City_ID=C.City_ID
91) Write a Query to display Donor_ID, Donor_Name, Phone No,
Blood Group?
SELECT D.Donor_ID, D_Name, D_Phone No, B.Blood_Group
FROM Donor D
INNER JOIN Blood B
ON D.Blood_ID=B.Blood_ID;
92) Write a Query to display Donor_ID, Donor_Name, Phone No, and
who is staying in Hyderabad?
SELECT D.Donor_ID, D_Name, D_Phone No, C.City_Name
FROM Donor D
INNER JOIN City C
ON C.City_ID=D.City_ID
WHERE C.City_Name=’hyderabad’
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
93) Write a Query to display donor details whose blood group is A
+ve?
SELECT D.Donor_ID, D_Name, D_Phone No
FROM Donor D
INNER JOIN Blood B
ON D.Donor_ID=B.Blood_ID
WHERE B.Blood_Group=’A+ve’
94) Write a Query to display Donor_ID, Donor_Name, Phone No,
City, Blood Group?
SELECT D.Donor_ID, D_Name, D_Phone No, C.City_Name
B.Blood_Group
FROM Blood B
INNER JOIN Donor D
ON D.Blood_ID=B.Donor_Name
INNER JOIN City C
ON D.City_ID=C.City_ID
95) Write a Query to display Donor_Name, Phone No, Blood Group
of the donors who are staying in Hyderabad and whose blood group
is A+ve?
SELECT D.Donor_Name, D. Phone_Number, B.Blood_Group
FROM Donor D
INNER JOIN Blood B
ON D.Blood_ID=B.Blood_ID
INNER JOIN City C
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
ON D.City_ID=C.City_ID
WHERE C.City_Name=’hyderabad’ and B.Blood_Group=’A+ve’
Outer Join A join that includes rows even if they do not have related
rows in the joined table is an Outer Join. You can create three
different outer joins to specify the unmatched rows to be included:
Dept_No Depat_Name
10 EEE
20 EEE
30 CSE
Null Null
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
50 IT
96) Write a Query to display only left records?
SELECT e.*
FROM Employee E
LEFT OUTER JOIN Department D
ON E.D_no
WHERE D.D_No IS NULL
97) Write a Query to display employee details where employee no is
101?
SELECT *
FROM Employee E
WHERE E_No=101
98) Write a Query to display employee details where the employee
number is null?
SELECT *
FROM Employee E
WHERE E_No IS NULL
99) Write a Query to display only the right records?
SELECT D.*
FROM Employee E
RIGHT OUTER JOIN Department D
ON E.D.No=D.D_No
WHERE E.D_No IS NULL
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
100) Write a Query to display all the records from the table except
matching records?
SELECT E.*, D.*
FROM Employee E
FULL JOIN Department D
ON E.D_No=D.D_No
WHERE E.D_No IS NULL or D.D_No IS NULL
Department Details Table
Dept_No Dept_Name
1 ECE
2 CSE
3 EEE
Course Details Table
Course_ID Course_Name Cr
1 EDC 4
2 PDC 4
3 SS 4
4 DAA 4
5 OS 4
Student Details Table
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
Student_No Student_Name
101 Anil
102 Sunil
103 Ajay
104 Vijay
105 John
Enroll Details Table
Emp_No Address
E1 Hyderabad
E2 Vizag
E3 Hyderabad
E4 Bangalore
E5 Hyderabad
Employee Details Table
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
Emp_No Emp_Name
E1 Arun
E2 Kiran
E3 Kumar
E4 Anus
E5 James
Semester Details Table
Semester Sn
S1 1
S2 2-1
S3 2-2
S4 3-1
S5 3-2
S6 4-1
S7 4-2
Course Department Details
Dept_No Course_ID
10 1
10 2
10 3
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
20 4
20 5
Syllabus Table
Emp_No Dept_No
E1 10
E2 10
E3 10
E4 20
E5 30
Course Instructor Table
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
1 E2 S1 30
2 E3 S1 10
4 E4 S2 20
5 E4 S2 20
5 E5 S1 10
101) Write a query to display Student No, Student Name, Enroll Date,
Department Name?
SELECT S.Student_No, S.Student_Name, S.Enroll_Date,
D.Dept_Name
FROM Student S
INNER JOIN Enroll E
ON S.Student_No=E.Student_No
INNER JOIN Department D
ON D.Dept_No=E.Dept_No
102) Write a query to display Employee Number, Employee Name
and address, department name?
SELECT E.Emp_No, E.Emp_Name, A.Address, D.Dept_Name
FROM Employee E
INNER JOIN Address A
ON E.Emp_No=A.Emp_No
INNER JOIN Instructor I
ON A.Emp_No=I.Emp_No
INNER JOIN Department D
ON I.Dept_No=D.Dept_No
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
103) Write a query to display the course name belongs to the ECE
department?
SELECT C.Course_Name
FROM Department D
INNER JOIN Course Department CD
ON D.Dept_NO=CD.Dept_NO
INNER JOIN Course C
ON CD.CourseDept_ID=C.Course_ID
WHERE D.Dept_Name=’ECE’
104) Write a query to display student number, student name, enroll
date, dept name, semester name?
SELECT S.Student_No, S.Student_Name, S.Enroll_Date,
D.Dpet_Name, Sem.Student_Name
FROM Enroll E
INNER JOIN Student S
ON S.Student_No=E.Student_No
INNER JOIN Deprtment D
ON E.Dept_No=D.Dept_No
INNER JOIN Semester SE
ON E.Student_ID=Sem.Student_ID
105) Write a query to display the syllabus of the ECE department's
1st year?
SELECT C.Course_Name
FROM Department D
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
INNER JOIN Syllabus Sy
ON D.Dept_No=Sy.Dept_No
INNER JOIN Course C
ON Sy.Course_ID=C.Course_ID
INNER JOIN Semester Se
ON Syllabus_Sy_ID=Se_Sy_ID
WHERE D.Dept_Name=’ECE’ and Se.Semester=’1’
106) Write a query to display the employee names and faculty names
of ECE dept 1st year?
SELECT E.Emp_Name
FROM Employee E
INNER JOIN Course Instructor Ci
ON E.Emp_No=Ci.Emp_No
INNER JOIN Semester Se
ON Se.Student_ID=Ci.Student_ID
INNER JOIN Dept D
ON Ci.Dept_No=D.Dept_No
WHERE D.Dept_Name=’ECE’ and Se.Student_Name=’1’
107) Write a query to display student details who enrolled for the
ECE department?
SELECT S.Student_NO, S.Student_Name, S.Enroll_Date
FROM Student S
INNER JOIN Enroll E
ON S.Student_No=E.Student_No
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
INNER JOIN Department D
ON E.Dept_No=D.Dept_No
WHERE D.Dept_Name=’ECE’
108) Write a query to display student details along with dept name
who are enrolled in the ECE department the first year?
SELECT S.Student_No, S.Student_Name, S.Enroll_Date, D.Dept_Name
FROM Student S
INNER JOIN Enrollment E
ON S.Student_No=E.Student_No
INNER JOIN Department D
ON D.Dept_No=E.Dept_No
INNER JOIN Semester Se
ON E.Student_ID=Se.Student_ID
WHERE D.Dept_Name=’ECE’ and Se.Student_Name=’1’
109) Write a query to display the employee name who is teaching
EDC?
SELECT E.Emp_Name
FROM Employee E
INNER JOIN Course Instructor Ci
ON E.Emp_No=Ci.Emp_No
INNER JOIN Course C
ON Ci.Course_ID=C.Course_ID
WHERE C.Course_Name=’EDC’
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
110) Write a query to display employee details along with dept name
who are staying in Hyderabad?
SELECT E.Emp_No, Emp_Name, D.Dept_Name
FROM Employee E
INNER JOIN Address A
ON E.Emp_No=A.Emp_No
INNER JOIN Instructor I
ON A.Emp_No=I.Emp_No
INNER JOIN Department D
ON I.Dept_No=D.Dept_No
WHERE A.Address=’hyderabad’
Emp_No Emp_Name Salary Age Dept_Name
101 Anil 20,000 22 ECE
102 Sunil 23000 23 EEE
103 Vijay 32000 24 CSE
Using Range Operator:: BETWEEN, NOT BETWEEN
111) Write a Query to display employee details whose salary > 20000
and whose age >23?
SELECT * FROM Employee
WHERE Salary>20000 AND Age>23;
112) Write a Query to display employee details whose salary >20000
and who is working in the ECE department?
SELECT * FROM Employee
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
WHERE Salary>20000 AND Dept_Name=’ECE’
113) Write a Query to display employee details whose age is
BETWEEN 18 and 22?
SELECT * FROM Employee Details
WHERE Age BETWEEN 18 AND 22;
114) Write a Query to display employee details whose salary range
BETWEEN 20000 and 23000?
SELECT * FROM Employee
WHERE Salary BETWEEN 20000 AND 23000;
115) Write a Query to display employee details whose age is NOT
BETWEEN 18 & 22?
SELECT * FROM Employee
WHERE Age NOT BETWEEN 18 AND 22;
Using String Operators:: LIKE, NOT LIKE
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194
RN REDDY IT SCHOOL
WHERE Salary>20000 AND Age>20 AND Emp_Name LIKE ‘a%’
118) Write a Query to display employee details whose name not
starts with a?
SELECT * FROM employee
WHERE Emp_Name NOT LIKE ‘a%’
#102, KVR Enclave, Near Gurudwar Temple, Ameerpet, Hyderabad - 500016. Website:
www.rnreddyitschool.co Email ID: rnreddyitschool@gmail.com, Ph: 9866183094,
9866193194