DBMS Questions
DBMS Questions
DBMS Questions
Ans: An RDBMS is a system that organizes data into tables called relations, which
are further organized into columns (fields) and rows (often called tuples). The
relational model allows data to be queried in a nearly unlimited number of ways,
making it great for sorting through large volumes of data. It’s important to pick a
SQL developer who’s experienced with the particular set of web technologies you
plan to use to support your app. Common SQL dialects include PL/SQL for Oracle,
T-SQL for MS SQL, and JET SQL for MS Access.
2. What are the standard SQL commands every SQL developer should know?
Ans: The basic SQL commands can be organized into the following categories:
In practice however, you should be aware that your typical developer is most likely
going to answer this question with CRUD (Create, Read, Update, and Delete), the
four essential database operations for database manipulation. Bonus points if they
also mention some of the others.
3. Can you explain how a RDBMS organizes data into tables and fields?
Ans: A table is composed of columns (fields) and rows (records or tuples). Each
record can be considered as an individual entry that exists within the table and
contains multiple fields. For example, a data entry (record) for a customer might
consist of the fields: ID, name, address, and purchase.
4. What is a NULL value and how does it differ from a zero value?
Ans: The easiest way to explain this difference is to recognize that zero is a value
representing the number zero. NULL is a non-value or a placeholder for data that is
not currently known or specified. The result of any operation on a NULL value, as in
arithmetic, will be undefined.
5. What are SQL Constraints?
Ans: Constraints are rules that can be placed on columns or tables to limit the type
of data that can be entered into a table. This prevents errors and can improve the
accuracy and reliability of the database as a whole.
Ans: When it comes to storing data accurately, consistently, and reliably within a
RDBMS, there are four general types of data integrity that you can implement:
8. Explain the difference between an inner join and outer join using an
example.
Ans: An inner join is when you combine rows from two tables and create a result set
based on the predicate, or joining condition. The inner join only returns rows when it
finds a match in both tables. An outer join will also return unmatched rows from one
table if it is a single outer join, or both tables if it is a full outer join. A solid example
of this will clearly illustrate the difference and demonstrate how well the developer
understands joins.
9. What is wrong with the SQL query below?
SELECT UserId, AVG(Total) AS AvgOrderTotal
FROM Invoices
HAVING COUNT(OrderId) >= 1;
Ans: The issue here is that there must be a GROUP BY clause here. This query will
get the average order amount by customer (UserId) where the customer has at least
1 order. The correct query is listed below:
SELECT UserId, AVG(Total) AS AvgOrderTotal
FROM Invoices
GROUP BY Userid
HAVING COUNT(OrderId) >= 1
10. Consider the two tables below. Write a query that retrieves all
employees recruited by John Do. How would you write a second query to
retrieve all employees that were not recruited by any recruiter?
Employee Table
Id Name RecruitedBy
1 Jean Grayson NULL
2 Paul Smith 1
3 John Do NULL
4 Alex Lee 3
5 Lisa Kim 3
6 Bob Thompson NULL
Recruiter Table
Id Name
1 Bob Smith
2 Paul Allen
3 John Do
Ans:
The following query will retrieve all recruiters recruited by John Do.
SELECT Employee.Name FROM Employee
JOIN Recruiter ON Employee.RecruitedBy = Recruiter.Id
WHERE RecruitedBy = 3
To retrieve all employees who were not recruited by anyone in the recruiter table,
you could use the following query:
SELECT Employee.Name FROM Employee
JOIN Recruiter ON Employee.RecruitedBy = Recruiter.Id
WHERE RecruitedBy Is Null;
11. Write a SQL query to find the 10th tallest peak (“Elevation”) from a
“Mountain” table. Assume that there are at least 10 records in the Mountain
table. Explain your answer.
The first query takes the top 10 mountains by elevation in the table and lists them in
descending order, with the tallest mountain at the top of the list. However, since we
want the 10th tallest mountain, the second query, “ AS Mount ORDER BY
Elevation”, promptly reorders the list of 10 in ascending order before the top record
is selected. Note that not all databases support the “TOP” keyword, so answers may
vary. Another possible solution that follows a similar logic for MySQL or PostreSQL
is detailed below, this time using the “LIMIT” keyword.
SELECT Elevation FROM
(
SELECT DISTINCT Elevation FROM Mountain ORDER BY Elevation DESC LIMIT 10
) AS Mt ORDER BY Elevation LIMIT 1;
12. Given two tables created in the code block below, how would you write a
query to fetch values in table “fibonacci” that are not in table “prime” without
using the “NOT” keyword? Can you name a database technology where this is
not possible?
create table fibonacci(id numeric);
create table prime(id numeric);
insert into fibonacci(id) values(2), (3), (5), (8), (13), (21);
Ans:
SQLite, PostgreSQL, and SQL Server all support the ever useful “except” keyword
which can be employed as detailed below
select * from fibonacci
except
select * from prime;
A popular database technology that does not support “except” is MySQL, which is
why it must use the “not in” keyword. Note that for Oracle, the “minus” keyword must
be used instead.