You are on page 1of 16

6/4/2018 12 Essential SQL interview questions and answers - Upwork™

12 SQL interview
questions and answers
Looking for SQL freelancers to build your team?
Check out Upwork's top SQL Programmers (https://www.upwork.com/hire/sql-
programmers/)

Get Started (https://www.upwork.com/signup/)

Q What is a Relational Database Management System (RDBMS), and


which one are you most familiar with?

A A 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. Look up any particular
dialects used for your chosen RDBMS.

HIDE THE ANSWER

Q What are the standard SQL commands every SQL developer should
know?

A The basic SQL commands can be organized into the following categories:

https://www.upwork.com/i/interview-questions/sql/ 1/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

Data Manipulation Language (DML)

INSERT: Creates records. The “Create” in CRUD.

SELECT: Retrieves records. The “Read” in CRUD.

UPDATE: Modifies records. The “Update” in CRUD.

DELETE: Deletes records. The “Delete” in CRUD.

Data Definition Language (DDL)

CREATE: Creates a new object.

ALTER: Alters an existing object.

DROP: Deletes an existing object.

Data Control Language: (DCL)

GRANT: Grants privileges to users.

REVOKE: Revokes privileges previously granted to a user.


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.

HIDE THE ANSWER

Q Can you explain how a RDBMS organizes data into tables and fields?

https://www.upwork.com/i/interview-questions/sql/ 2/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

A 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.

HIDE THE ANSWER

Q What is a NULL value and how does it differ from a zero value?

A 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.

HIDE THE ANSWER

Q What are SQL Constraints?

A Constraints are rules you can place 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. Common constraints include:

NOT NULL: Prevents a column from having a NULL value.

DEFAULT: Specifies a default value for a column where none is specified.

PRIMARY KEY: Uniquely identifies rows/records within a database table.

FOREIGN KEY: Uniquely identifies rows/records from external database


tables.

UNIQUE: Ensures all values are unique.

CHECK: Checks values within a column against certain conditions.

https://www.upwork.com/i/interview-questions/sql/ 3/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

INDEX: Quickly creates and retrieves data from a database.

HIDE THE ANSWER

Q Name four ways to maintain data integrity within a RDBMS.

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

Entity (Row) Integrity: Avoids duplicate rows in tables.

Domain (Column) Integrity: Restricts the type, format, or range of values to


enforce valid entries.

Referential Integrity: Ensures rows used by other records cannot be


deleted.

User-Defined Integrity: Enforces rules set by the user that do not fall into
the other categories.

HIDE THE ANSWER

Q What is the purpose of database normalization and how does it work?

A The primary purpose of normalization is to make databases more efficient by


eliminating redundant data and ensuring data dependencies are coherent.
Storing data logically and efficiently reduces the amount of space the database
takes up and improves performance. The set of guidelines used to achieve
normalization are called normal forms, numbered from 1NF to 5NF. A form can be
thought of as a best-practice format for laying out data within a database.

HIDE THE ANSWER

https://www.upwork.com/i/interview-questions/sql/ 4/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

Q Explain the difference between an inner join and outer join using an
example.

A 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.

HIDE THE ANSWER

Q What is wrong with the SQL query below?

1 SELECT UserId, AVG(Total) AS AvgOrderTotal


2 FROM Invoices
3 HAVING COUNT(OrderId) >= 1

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

1 SELECT UserId, AVG(Total) AS AvgOrderTotal


2 FROM Invoices
3 GROUP BY Userid
4 HAVING COUNT(OrderId) >= 1

HIDE THE ANSWER

Q 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

https://www.upwork.com/i/interview-questions/sql/ 5/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

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

A The following query will retrieve all recruiters recruited by John Do.
SELECT Employee.

1 Name FROM Employee


2 JOIN Recruiter ON Employee.RecruitedBy = Recruiter.Id
3 WHERE RecruitedBy = 3

To retrieve all employees who were not recruited by anyone in the recruiter table,
you could use the following query:

1 SELECT Employee.Name FROM Employee


2 JOIN Recruiter ON Employee.RecruitedBy = Recruiter.Id
3 WHERE RecruitedBy Is Null

HIDE THE ANSWER

Q 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.

A This can be accomplished using the “TOP” keyword as follows.

https://www.upwork.com/i/interview-questions/sql/ 6/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

1 SELECT TOP (1) Elevation FROM


2 (
3 SELECT DISTINCT TOP (10) Elevation FROM Mountain ORDER BY Elevati
4 ) AS Mt ORDER BY Elevation

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.

1 SELECT Elevation FROM


2 (
3 SELECT DISTINCT Elevation FROM Mountain ORDER BY Elevation DESC L
4 ) AS Mt ORDER BY Elevation LIMIT 1;

HIDE THE ANSWER

Q 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?

1 create table fibonacci(id numeric);


2 create table prime(id numeric);
3
4 insert into fibonacci(id) values
5 (2),
6 (3),
7 (5),
8 (8),
9 (13),
10 (21);
11
12 insert into prime(id) values
13 (2),
14 (3),
15 (5),
16 (13);

A SQLite, PostgreSQL, and SQL Server all support the ever useful “except” keyword
which can be employed as detailed below
https://www.upwork.com/i/interview-questions/sql/ 7/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

1 select * from fibonacci


2 except
3 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.

HIDE THE ANSWER

Submit a SQL interview question


Submitted questions and answers are subject to review and editing, and may or may not
be selected for posting, at the sole discretion of Upwork.

Question

Question

Answer

Answer

Submit

At a glance: SQL Programmers

https://www.upwork.com/i/interview-questions/sql/ 8/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

Structured Query Language, better known as SQL, is a standard programming language


used to store, retrieve, and otherwise manage data contained within a relational database.
If you plan to power the backend of your website using Microsoft SQL, Informix, IBM DB2,
MySQL, Oracle, MS Access, Sybase, or another relational database management system, a
SQL developer can help bring your web project to life. Here are some well-crafted SQL
interview questions that can help you find the right SQL developer for your needs.

Hire a top SQL Programmer

(/o/profiles/users/_~017b90cdc12bedc13a/)

NELSON T.
(/o/profiles/users/_~017b90cdc12bedc13a/)
Web Developer

Hourly Rate $50/hr


Location United States

Job Success 100%

SQL JavaScript PHP jQuery CSS

View Profile (/o/profiles/users/_~017b90cdc12bedc13a/)

https://www.upwork.com/i/interview-questions/sql/ 9/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

(/o/profiles/users/_~017627e62d8941926c/)

OSSIAN R.
(/o/profiles/users/_~017627e62d8941926c/)
Software Consultant

Hourly Rate $70/hr


Location United States

Job Success 100%

SQL JavaScript PHP HTML C

View Profile (/o/profiles/users/_~017627e62d8941926c/)

(/o/profiles/users/_~017281654cd8dcb2ee/)

SANJA K.
(/o/profiles/users/_~017281654cd8dcb2ee/)
Email Marketing Expert & IT Consultant

Hourly Rate $50/hr


Location Czech Republic

Job Success 94%

SQL C# HTML CSS ASP.NET

https://www.upwork.com/i/interview-questions/sql/ 10/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

View Profile (/o/profiles/users/_~017281654cd8dcb2ee/)

(/o/profiles/users/_~013bcc7a64dde8167d/)

BILAL S.
(/o/profiles/users/_~013bcc7a64dde8167d/)
expert web developer/PHP/wordpress/HTML/CSS

Hourly Rate $15/hr


Location Pakistan

Job Success 100%

SQL JavaScript PHP HTML HTML5

View Profile (/o/profiles/users/_~013bcc7a64dde8167d/)

(/o/profiles/users/_~014f37d91cb11de207/)

ALEKSANDAR I.
(/o/profiles/users/_~014f37d91cb11de207/)
Senior C# .Net Desktop Developer

Hourly Rate $32/hr


Location Macedonia

Job Success 100%

SQL C# HTML CSS .NET Framework

https://www.upwork.com/i/interview-questions/sql/ 11/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

View Profile (/o/profiles/users/_~014f37d91cb11de207/)

(/o/profiles/users/_~01cab2b31542a48296/)

SAQIB Z.
(/o/profiles/users/_~01cab2b31542a48296/)
Full Stack Web Developer | Software Engineer

Hourly Rate $20/hr


Location Pakistan

Job Success 95%

SQL JavaScript PHP jQuery HTML5

View Profile (/o/profiles/users/_~01cab2b31542a48296/)

(/o/profiles/users/_~016974c25bd11d07f1/)

MOHSIN Z.
(/o/profiles/users/_~016974c25bd11d07f1/)
Microsoft Database Engineer (Access/SQL)

Hourly Rate $50/hr


Location Pakistan

Job Success 100%

https://www.upwork.com/i/interview-questions/sql/ 12/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

SQL PHP ASP.NET MVC Microsoft Access Administration Microsoft Excel

View Profile (/o/profiles/users/_~016974c25bd11d07f1/)

(/o/profiles/users/_~01098641b6ad2a5cc0/)

ALEXANDER S.
(/o/profiles/users/_~01098641b6ad2a5cc0/)
Full stack web-developer

Hourly Rate $46/hr


Location Russia

Job Success 100%

SQL JavaScript PHP HTML CSS

View Profile (/o/profiles/users/_~01098641b6ad2a5cc0/)

Sign up for FREE


to see more great
freelancers

https://www.upwork.com/i/interview-questions/sql/ 13/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

Get Started (/signup/?dest=job-posting)

Bring your idea to life with a SQL


Programmer

Get Started (/signup/?dest=home)

Top-skilled freelancers and the essentials to find and work with them.

Learn more (/)

Personalized assistance to help you find premium, pre-vetted talent.

Learn more (/pro/?channel=footer)

https://www.upwork.com/i/interview-questions/sql/ 14/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

End-to-end technology and service solution customized to fit your company.

Learn more (/enterprise/)

COMPANY

About Us (/about/)

Contact (https://support.upwork.com/hc/en-us)

Careers (/about/careers/)

Press (/press/)

Trust & Safety (/i/trust-safety-mission/)

Terms of Service (/legal/)

Privacy Policy (/legal/privacy/)

RESOURCES

Help Center (https://support.upwork.com/hc/en-us)

Hiring Headquarters (/hiring/)

Hiring Resources (/i/hiring-guides/)

Upwork Blog (/blog/)

Customer Stories (/ppc/video-portal/)

Business Resources (/i/business-resources/)

Payroll Services (/i/payroll-client/)

BROWSE

Freelancers by Skill (/i/freelancer-categories/)

Freelancers in USA (/local/united-states/)

Freelancers in UK (/local/united-kingdom/)

Freelancers in Canada (/local/canada/)

Freelancers in Australia (/local/australia/)

Jobs in USA (/freelance-jobs/united-states/)

Find Jobs (/i/job-categories/)

https://www.upwork.com/i/interview-questions/sql/ 15/16
6/4/2018 12 Essential SQL interview questions and answers - Upwork™

FOLLOW US

(https://plus.google.com/107040851368295259701)
(https://www.facebook.com/upwork)
(https://www.linkedin.com/company/upwork)
(https://twitter.com/Upwork)
(https://www.youtube.com/channel/UCvxGFOnwUBDHHcxuPqhe4CQ)
(https://www.instagram.com/upworkinc/)

MOBILE APP

(https://itunes.apple.com/us/app/upwork-
messenger/id944344799?
(https://play.google.com/store/apps/details?
mt=8)id=com.upwork.android&hl=en)

© 2015 - 2018 Upwork Global Inc.

https://www.upwork.com/i/interview-questions/sql/ 16/16

You might also like