SQL for Data Analysis
- 33Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What does SQL stand for and what is its purpose?
33 questions
- 1
What does SQL stand for and what is its purpose?
Model answer
SQL stands for Structured Query Language. It is a language used to interact with databases, allowing users to retrieve, filter, sort, add, update, and remove data.
Explanation
SQL is the standard language for managing relational databases. It enables users to perform various operations on data stored in tables.
- 2
What is a database and what are its basic components?
Model answer
A database is a collection of tables that have relationships with each other. Tables contain fields (columns) and records (rows).
Explanation
Databases organize data into related tables, minimizing redundancy. Each table represents an entity, and fields store specific attributes.
- 3
What is an RDBMS and name three popular examples.
Model answer
An RDBMS (Relational Database Management System) is software used to manage relational databases. Examples include Oracle, PostgreSQL, Microsoft SQL Server, and MySQL.
Explanation
RDBMS provide tools for creating, querying, and administering databases. They enforce relationships and integrity through constraints.
- 4
What is the difference between the Express and Developer editions of Microsoft SQL Server?
Model answer
The Express edition can be used for business purposes but is limited to 10 GB database size and 4 cores. The Developer edition has full functionality but can only be used for development and testing.
Explanation
Express is suitable for small-scale applications, while Developer is meant for non-production environments like learning and development.
- 5
How do you verify that SQL Server is running after installation?
Model answer
Open SQL Server Configuration Manager and check that the SQL Server service is running. The start mode should be set to automatic so it starts when the PC boots.
Explanation
Configuration Manager allows you to manage SQL Server services, including start, stop, and restart operations.
- 6
What is SQL Server Management Studio (SSMS) and why is it used?
Model answer
SSMS is a graphical front-end tool that allows users to connect to SQL Server instances and write queries, manage databases, and view data.
Explanation
SSMS provides an interface for interacting with SQL Server without needing to use command-line tools.
- 7
Why is it better to break a database into multiple related tables instead of using one large file?
Model answer
Breaking data into separate tables avoids repetition of information, saves storage space, and makes it easier to update data. This process is called normalization.
Explanation
Normalization reduces redundancy and improves data integrity by storing each piece of information in only one place.
- 8
What is a primary key?
Model answer
A primary key is the minimum number of columns needed to uniquely identify each record in a table.
Explanation
Primary keys ensure that each row is distinct, enabling efficient lookups and relationships between tables.
- 9
In the KCC database, what is the primary key in the Customers table and why is it not the customer name?
Model answer
The primary key is CustomerID. Customer name cannot be used because it is not unique (e.g., there are two customers named 'Tres Delicious').
Explanation
Primary keys must uniquely identify each record. Names can repeat, so a surrogate key like an ID is used.
- 10
What is a schema and what is the default schema in SQL Server?
Model answer
A schema is a way to organize tables into groups and manage access permissions. The default schema is 'dbo'.
Explanation
Schemas help in logically separating database objects, and 'dbo' stands for database owner.
- 11
What is the difference between a field and a record in a database table?
Model answer
A field is a column that stores a specific type of data, while a record is a row that represents a single entry with values for each field.
Explanation
Fields define the structure; records contain the actual data. For example, in a Customers table, fields include CustomerName, Address, etc., and each row is one customer.
- 12
Write a SQL query to select all customer names from the Customers table.
Model answer
SELECT CustomerName FROM dbo.Customers;
Explanation
The SELECT statement retrieves data. The FROM clause specifies the table. Using the schema prefix dbo is optional but good practice.
- 13
How can you retrieve all columns from a table without listing each column name?
Model answer
Use an asterisk (*) in the SELECT statement: SELECT * FROM dbo.Customers;
Explanation
The asterisk is a wildcard that returns all columns. It is convenient but not recommended for production queries as it may return unnecessary data.
- 14
What is the purpose of the DISTINCT keyword?
Model answer
DISTINCT removes duplicate rows from the result set, returning only unique values for the selected columns.
Explanation
For example, SELECT DISTINCT CustomerName FROM dbo.Customers; returns each customer name only once, even if it appears multiple times.
- 15
How can you limit the number of rows returned by a query?
Model answer
Use the TOP keyword followed by the number of rows: SELECT TOP 3 * FROM dbo.Customers;
Explanation
TOP restricts the result to the first N rows. It is useful for sampling large tables.
- 16
How do you rename a column in the output using an alias?
Model answer
Use the AS keyword: SELECT CustomerName AS [Customer Name] FROM dbo.Customers;
Explanation
Aliases allow you to display a custom column header. Brackets are used if the alias contains spaces.
- 17
Why would you include the database name in a query like 'KCC.dbo.Customers'?
Model answer
Including the database name ensures the query runs correctly even if the active database is different, preventing errors due to wrong context.
Explanation
When working with multiple databases, specifying the full path avoids ambiguity and errors if the table does not exist in the current database.
- 18
How do you filter rows in a query to find customers from a specific state?
Model answer
Use the WHERE clause: SELECT * FROM dbo.Customers WHERE State = 'Washington';
Explanation
WHERE filters rows based on a condition. The condition uses an operator (here, =) to compare a column value.
- 19
What is the difference between AND and OR in a WHERE clause?
Model answer
AND requires all conditions to be true, while OR requires at least one condition to be true.
Explanation
AND narrows the result; OR expands it. For example, WHERE State = 'WA' AND Country = 'US' returns rows that satisfy both.
- 20
How can you filter for rows where a value is within a list of options?
Model answer
Use the IN operator: SELECT * FROM dbo.Customers WHERE State IN ('Washington', 'New York', 'Utah');
Explanation
IN is a shorthand for multiple OR conditions. It checks if the column value matches any value in the list.
- 21
How do you filter rows where a column value is not in a list?
Model answer
Use NOT IN: SELECT * FROM dbo.Customers WHERE State NOT IN ('Washington', 'New York', 'Utah');
Explanation
NOT IN excludes rows that match any value in the list.
- 22
How can you find rows where a text column starts with a specific letter?
Model answer
Use the LIKE operator with a wildcard: SELECT * FROM dbo.Customers WHERE CustomerName LIKE 'A%';
Explanation
The percent sign (%) represents any number of characters. 'A%' matches any customer name starting with 'A'.
- 23
How do you write a comment in a SQL query?
Model answer
Use two dashes (--) for single-line comments or /* */ for multi-line comments.
Explanation
Comments help document the query for readability. They are ignored by the SQL engine when executing.
- 24
How can you sort query results in ascending order by a column?
Model answer
Use ORDER BY: SELECT * FROM dbo.Orders ORDER BY OrderTotal; (ascending is default).
Explanation
ORDER BY sorts the result set. Add DESC for descending order.
- 25
What is an INNER JOIN and what does it return?
Model answer
An INNER JOIN returns only rows that have matching values in both joined tables. It excludes rows without a match.
Explanation
For example, joining Orders and Customers on CustomerID returns only orders that have a valid customer.
- 26
What is the difference between LEFT OUTER JOIN and RIGHT OUTER JOIN?
Model answer
LEFT OUTER JOIN returns all rows from the left table, even if there is no match in the right table. RIGHT OUTER JOIN returns all rows from the right table, regardless of matches.
Explanation
In a left join of Orders (left) and Customers (right), all orders are shown; if a customer is missing, customer columns show NULL.
- 27
Why might you get an 'ambiguous column name' error when joining tables and how do you fix it?
Model answer
The error occurs when a column name exists in both tables. Fix it by prefixing the column with the table name or alias (e.g., C.CustomerID).
Explanation
When columns share a name, the SQL engine cannot determine which table to use. Aliases like C for Customers clarify the source.
- 28
Write a SQL query using table aliases to join Orders and Customers, selecting OrderID, OrderDate, CustomerName, and Phone.
Model answer
SELECT O.OrderID, O.OrderDate, C.CustomerName, C.Phone FROM dbo.Orders AS O INNER JOIN dbo.Customers AS C ON O.CustomerID = C.CustomerID;
Explanation
Aliases (O and C) make the query shorter and clearer. The ON clause specifies the join condition.
- 29
How can you use the DATEADD function to get orders from the last month without hardcoding a date?
Model answer
SELECT * FROM dbo.Orders WHERE OrderDate >= DATEADD(month, -1, GETDATE());
Explanation
DATEADD adds an interval to a date. -1 month and GETDATE() gives the current date, so it returns dates from the past month.
- 30
What does the COUNT function do and how is it used?
Model answer
COUNT returns the number of rows that match the query. For example: SELECT COUNT(*) FROM dbo.Orders WHERE OrderDate >= DATEADD(month, -1, GETDATE());
Explanation
COUNT is an aggregate function. COUNT(*) counts all rows; COUNT(ColumnName) counts non-NULL values in that column.
- 31
How can you calculate the total sales from orders in the last month?
Model answer
SELECT SUM(OrderTotal) FROM dbo.Orders WHERE OrderDate >= DATEADD(month, -1, GETDATE());
Explanation
SUM adds up the values in a numeric column. Here it sums all order totals for the specified period.
- 32
How do you group aggregated results by a specific column?
Model answer
Use GROUP BY: SELECT CustomerID, SUM(OrderTotal) FROM dbo.Orders GROUP BY CustomerID;
Explanation
GROUP BY divides rows into groups based on column values, allowing aggregate functions to calculate per group.
- 33
What is the Query Designer and how can it help write complex queries?
Model answer
The Query Designer in SSMS is a visual tool that allows you to select tables, choose columns, add filters, and set sorting/grouping; it automatically generates the SQL code.
Explanation
You can right-click in a query window and select 'Design Query in Editor' to open the designer, making query building easier for beginners.

Ask anything
We've read this whole study set, so questions about it get the best answers.