SQL Interview Questions1
SQL Interview Questions1
SQL Interview Questions1
2. What is RDBMS ?
Relational Database Management system (RDBMS) is a database management
system (DBMS) that is based on the relational model. Data from relational
database can be accessed or reassembled in many different ways without having
to reorganize the database tables. Data from relational database can be
accessed using an API , Structured Query Language (SQL).
3. What is SQL ?
Institute) standard.
This is one of the most frequently asked SQL Interview Questions for freshers.
SQL statements are broadly classified into three. They are
1. DDL Data Definition Language
DDL is used to define the structure that holds the data. For example, Create, Alter,
Update and retrieving the data from the table. The Select statement is considered as a
limited version of the DML, since it can't change the data in the database. But it can
perform operations on data retrieved from the DBMS, before the results are returned to
DCL is used to control the visibility of data like granting database access and set
privileges to create tables, etc. Example - Grant, Revoke access permission to the user
every major DBMS supports SQL, so learning this one language will enable
programmers to interact with any database like ORACLE, SQL ,MYSQL etc.
2. SQL is easy to learn. The statements are all made up of descriptive English words,
3. SQL is actually a very powerful language and by using its language elements you
A record is the collection of values / fields of a specific entity: i.e. an Employee, Salary
etc.
A table is a collection of records of a specific type. For example, employee table, salary
table etc.
Database transaction takes database from one consistent state to another. At the
end of the transaction the system must be in the prior state if the transaction fails
or the status of the system should reflect the successful completion if the
transaction goes through.
completed, it will get reflected in DB or if any step fails, all the transactions are rolled
back.
2. Consistency
The database will move from one consistent state to another, if the transaction
3. Isolation
4. Durability
Database lock tells a transaction, if the data item in questions is currently being used by
other transactions.
1. Shared Lock
When a shared lock is applied on data item, other transactions can only read the item,
2. Exclusive Lock
When an exclusive lock is applied on data item, other transactions can't read or write
In database design, we start with one single table, with all possible columns. A lot of
redundant data would be present since its a single table. The process of removing
the redundant data, by splitting up the table in a well defined fashion is called
normalization.
A relation is said to be in first normal form if and only if all underlying domains contain
atomic values only. After 1NF, we can still have redundant data.
A relation is said to be in 2NF if and only if it is in 1NF and every non key attribute is
fully dependent on the primary key. After 2NF, we can still have redundant data.
3. Third Normal Form (3NF)
A relation is said to be in 3NF, if and only if it is in 2NF and every non key attribute is
A primary key is a column whose values uniquely identify every row in a table.
Primary key values can never be reused. If a row is deleted from the table, its primary
key may not be assigned to any new rows in the future. To define a field as primary key,
4. Value in a primary key column can never be modified or updated, if any foreign key
A Composite primary key is a type of candidate key, which represents a set of columns
in a table. What it means is that, a table which contains composite primary key will be
indexed based on the columns specified in the primary key. This key will be referred in
Name" in a table is required to uniquely identify a row, its called a Composite Primary
Key. In this case, both the columns will be represented as primary key.
When a "one" table's primary key field is added to a related "many" table in order to
create the common field which relates the two tables, it is called a foreign key in the
"many" table.
For example, the salary of an employee is stored in salary table. The relation is
SQL INSERT statement is used to add rows to a table. For a full row insert, SQL Query
should start with insert into statement followed by table name and values command,
followed by the values that need to be inserted into the table. The insert can be used in
several ways:
SQL Update is used to update data in a row or set of rows specified in the filter
condition.
The basic format of an SQL UPDATE statement is, Update command followed by table
to be updated and SET command followed by column names and their new values
SQL Delete is used to delete a row or set of rows specified in the filter condition.
The basic format of an SQL DELETE statement is, DELETE FROM command followed
by table name followed by filter condition that determines which rows should be
updated.
22. What are wild cards used in database for Pattern Matching ?
SQL Like operator is used for pattern matching. SQL 'Like' command takes more
time to process. So before using "like" operator, consider suggestions given
below on when and where to use wild card search.
1) Don't overuse wild cards. If another search operator will do, use it instead.
2) When you do use wild cards, try not to use them at the beginning of the search
pattern, unless absolutely necessary. Search patterns that begin with wild cards
are the slowest to process.
3) Pay careful attention to the placement of the wild card symbols. If they are
misplaced, you might not return the data you intended.
duplication, data is stored in related tables. Join keyword is used to fetch data from
related tables. "Join" return rows when there is at least one match in both table. Type of
joins are
Right Join
Return all rows from the right table, even if there are no matches in the left table.
Outer Join
Left Join
Return all rows from the left table, even if there are no matches in the right table.
Full Join
Self-join is query used to join a table to itself. Aliases should be used for the same
table comparison.
Cross Join will return all records where each row from the first table is combined with
The views are virtual tables. Unlike tables that contain data, views simply contain
Materialized views are also a view but are disk based. Materialized views get updates
on specific duration, base upon the interval specified in the query definition. We can
Advantages:
2. The view can be used to hide some of the columns from the table.
3. Views can provide Access Restriction, since data insertion, update and deletion is
Disadvantages:
bit slow.
3. When views are created for large tables, it occupies more memory.
Database triggers are sets of commands that get executed when an event(Before
retrieve data.
Once the truncate statement is executed, Commit and Rollback statement cannot be
performed. Where condition can be used along with delete statement but it can't be
Drop command is used to drop the table or keys like primary,foreign from a table.
33. What is the difference between Cluster and Non cluster Index?
A clustered index reorders the way records in the table are physically stored. There
can be only one clustered index per table. It makes data retrieval faster.
A non clustered index does not alter the way it was stored but creates a completely
separate object within the table. As a result insert and update command will be faster.
MINUS operator is used to return rows from the first query but not from the second
query. INTERSECT operator is used to return rows returned by both the queries.
+-------+
| total |
+-------+
| 100 |
+-------+
+----------------+
| cust_123_total |
+----------------+
| 15 |
+----------------+
Given the above query results, what will be the result of the
query below?
SELECT count(*) AS cust_not_123_total FROM orders WHERE customer_id <>
'123'
Hide answer
The obvious answer is 85 (i.e, 100 - 15). However, that is not necessarily correct.
Specifically, any records with a customer_id of NULL will not be included
in either count (i.e., they wont be included in cust_123_total , nor will they be
included in cust_not_123_total ). For example, if exactly one of the 100
customers has a NULL customer_id , the result of the last query will be:
+--------- ----------+
| cust_not_123_total |
+--------------------+
| 84 |
+--------------------+
What will be the result of the query below? Explain your
answer and provide a version that behaves correctly.
select case when null = null then 'Yup' else 'Nope' end as Result;
Hide answer
This query will actually yield Nope, seeming to imply that null is not equal to
itself! The reason for this is that the proper way to compare a value to null in
SQL is with the is operator, not with = .
Accordingly, the correct version of the above query that yields the expected result
(i.e., Yup) would be as follows:
select case when null is null then 'Yup' else 'Nope' end as Result;
return an empty set, even if there are many runner ids that match winner_ids in
the races table.
Knowing this, a query that avoids this issue would be as follows:
SELECT * FROM runners WHERE id NOT IN (SELECT winner_id FROM races WHERE
winner_id IS NOT null)
1 5 5
2 6 6
NULL 0 NULL
The EXISTS clause in the above query is a red herring. It will always be true
since ID is not a member of dbo.docs . As such, it will refer to
the envelope table comparing itself to itself!
The idnum value of NULL will not be set since the join of NULL will not return a
result when attempting a match with any value of envelope .
What is wrong with this SQL query? Correct it so it
executes properly.
SELECT Id, YEAR(BillingDate) AS BillingYear
FROM Invoices
WHERE BillingYear >= 2010;
Hide answer
The expression BillingYear in the WHERE clause is invalid. Even though it is
defined as an alias in the SELECT phrase, which appears before the WHERE
phrase, the logical processing order of the phrases of the statement is different
from the written order. Most programmers are accustomed to code statements
being processed generally top-to-bottom or left-to-right, but T-SQL processes
phrases in a different order.
The correct query should be:
SELECT Id, YEAR(BillingDate) AS BillingYear
FROM Invoices
This will return the same faulty set as the original. Why? We already covered
that: Anything compared to NULL evaluates to the third value in the three-valued
logic: UNKNOWN. That anything includes NULL itself! Thats why SQL Server
provides the IS NULL and IS NOT NULL operators to specifically check for NULL.
Those particular operators will always evaluate to true or false.
Even if a candidate doesnt have a great amount of experience with SQL Server,
diving into the intricacies of three-valued logic in general can give a good
indication of whether they have the ability learn it quickly or whether they will
struggle with it.
Considering the database schema displayed in the SQLServer-
style diagram below, write a SQL query to return a list of all the
invoices. For each invoice, show the Invoice ID, the billing date,
the customers name, and the name of the customer who
referred that customer (if any). The list should be ordered by
billing date.
Hide answer
SELECT i.Id, i.BillingDate, c.Name, r.Name AS ReferredByName
FROM Invoices i
ORDER BY i.BillingDate;
This question simply tests the candidates ability take a plain-English requirement and
write a corresponding SQL query. There is nothing tricky in this one, it just covers the
basics:
Did the candidate remember to use a LEFT JOIN instead of an inner JOIN when
joining the customer table for the referring customer name? If not, any invoices
by customers not referred by somebody will be left out altogether.
Did the candidate alias the tables in the JOIN? Most experienced T-SQL
programmers always do this, because repeating the full table name each time it
needs to be referenced gets tedious quickly. In this case, the query would
actually break if at least the Customer table wasnt aliased, because it is
referenced twice in different contexts (once as the table which contains the
name of the invoiced customer, and once as the table which contains the name
of the referring customer).
Did the candidate disambiguate the Id and Name columns in the SELECT?
Again, this is something most experienced programmers do automatically,
whether or not there would be a conflict. And again, in this case there would be
a conflict, so the query would break if the candidate neglected to do so.
Note that this query will not return Invoices that do not have an associated Customer.
This may be the correct behavior for most cases (e.g., it is guaranteed that every
Invoice is associated with a Customer, or unmatched Invoices are not of interest).
However, in order to guarantee that all Invoices are returned no matter what, the
Invoices table should be joined with Customers using LEFT JOIN:
FROM Invoices i
Hide answer
UPDATE SALARIES SET sex = CASE sex WHEN 'm' THEN 'f' ELSE 'm' END
Write a query to fetch values in table test_a that are and not
in test_b without using the NOT keyword.
Hide answer
In SQL Server, PostgreSQL, and SQLite, this can be done using the except keyword as
follows:
select * from test_a
except
Given a table TBL with a field Nmbr that has rows with the
following values:
1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1
Write a query to add 2 where Nmbr is 0 and add 3 where Nmbr is
1.
Hide answer
This can be done as follows:
update TBL set Nmbr = case when Nmbr > 0 then Nmbr+3 else Nmbr+2 end;
SELECT DISTINCT TOP (10) Salary FROM Employee ORDER BY Salary DESC
First, the SELECT DISTINCT TOP (10) Salary FROM Employee ORDER BY Salary DESC query
will select the top 10 salaried employees in the table. However, those salaries will be
listed in descending order. That was necessary for the first query to work, but now
picking the top 1 from that list will give you the highestsalary not the the 10th
highest salary.
Therefore, the second query reorders the 10 records in ascending order (which the
default sort order) andthen selects the top record (which will now be the lowest of those
10 salaries).
Not all databases support the TOP keyword. For example, MySQL and PostreSQL use
the LIMIT keyword, as follows:
SELECT Salary FROM
Write a SQL query using UNION ALL (not UNION ) that uses
the WHERE clause to eliminate duplicates. Why might you want to
do this?
Hide answer
You can avoid duplicates using UNION ALL and still run much faster than UNION
DISTINCT (which is actually same as UNION) by running a query like this:
SELECT * FROM mytable WHERE a=X UNION ALL SELECT * FROM mytable WHERE b=Y AND a!=X
The key is the AND a!=X part. This gives you the benefits of the UNION (a.k.a., UNION
DISTINCT ) command, while avoiding much of its performance hit.
user_id username
1 John Doe
2 Jane Don
3 Alice Jones
4 Lisa Romero
Write a query to to get the list of users who took the a training
lesson more than once in the same day, grouped by user and
training lesson, each ordered from the most recent lesson date
to oldest date.
Hide answer
SELECT
u.user_id,
username,
training_id,
training_date,
GROUP BY user_id,
training_id,
training_date
What is an execution plan? When would you use it? How would
you view the execution plan?
Hide answer
An execution plan is basically a road map that graphically or textually shows the data
retrieval methods chosen by the SQL servers query optimizer for a stored procedure or
ad hoc query. Execution plans are very useful for helping a developer understand and
analyze the performance characteristics of a query or stored procedure, since the plan
is used to execute the query or stored procedure.
In many SQL systems, a textual execution plan can be obtained using a keyword such
as EXPLAIN , and visual representations can often be obtained as well. In Microsoft SQL
Server, the Query Analyzer has an option called Show Execution Plan (located on the
Query drop down menu). If this option is turned on, it will display query execution plans
in a separate window when a query is run.
Given a table dbo.users where the column user_id is a unique
identifier, how can you efficiently select the first 100
odd user_id values from the table?
(Assume the table contains well over 100 records with
odd user_id values.)
Hide answer
SELECT TOP 100 user_id FROM dbo.users WHERE user_id % 2 = 1 ORDER BY user_id
How can you select all the even number records from a table?
All the odd number records?
Hide answer
To select all the even number records from a table:
Select * from table where id % 2 = 0
What are the NVL and the NVL2 functions in SQL? How do they
differ?
Hide answer
Both the NVL(exp1, exp2) and NVL2(exp1, exp2, exp3) functions check the
value exp1 to see if it is null.
With the NVL(exp1, exp2) function, if exp1 is not null, then the value of exp1 is
returned; otherwise, the value of exp2 is returned, but case to the same data type as
that of exp1 .
With the NVL2(exp1, exp2, exp3) function, if exp1 is not null, then exp2 is returned;
otherwise, the value of exp3 is returned.
What is the difference between
the RANK() and DENSE_RANK() functions? Provide an example.
Hide answer
The only difference between the RANK() and DENSE_RANK() functions is in cases where
there is a tie; i.e., in cases where multiple values in a set have the same ranking. In
such cases, RANK() will assign non-consecutive ranks to the values in the set
(resulting in gaps between the integer ranking values when there is a tie),
whereas DENSE_RANK() will assign consecutive ranks to the values in the set (so there
will be no gaps between the integer ranking values in the case of a tie).
For example, consider the set {25, 25, 50, 75, 75, 100} . For such a set, RANK() will
return {1, 1, 3, 4, 4, 6} (note that the values 2 and 5 are skipped),
whereas DENSE_RANK() will return {1,1,2,3,3,4} .