Practice Test

True/False: The SQL SELECT statement is mainly used to select data from a database.

  • True
  • False

Answer: True.

Explanation: The SELECT statement is used to select data from a database. The data returned is stored in a result table.

Which SQL statement is used to extract data from a database?

  • a) SELECT
  • b) EXTRACT
  • c) GET
  • d) PULL

Answer: a) SELECT.

Explanation: The SELECT statement is used to select or extract data from a database.

Can an SQL SELECT statement retrieve data from more than one table at a time without using a JOIN clause?

  • a) Yes
  • b) No

Answer: b) No.

Explanation: Without using a JOIN clause, an SQL SELECT statement can only retrieve data from one table at a time.

True/False: SQL JOIN clause is used to combine rows from two or more tables, based on a related column between them.

  • True
  • False

Answer: True.

Explanation: JOIN clause is used in SQL to combine rows from two or more tables based on a related column/field between them, referred as Foreign Key relationship.

In a SELECT statement with multiple qualifiers, what is the order of execution?

  • a) SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY
  • b) FROM, WHERE, SELECT, GROUP BY, HAVING, ORDER BY
  • c) FROM, SELECT, WHERE, GROUP BY, HAVING, ORDER BY
  • d) FROM, SELECT, WHERE, ORDER BY, GROUP BY, HAVING

Answer: b) FROM, WHERE, SELECT, GROUP BY, HAVING, ORDER BY.

Explanation: The order of execution for SQL operators starts with FROM, then WHERE, followed by SELECT, GROUP BY, HAVING, and finally ORDER BY.

True/False: AWS Redshift supports standard SQL including multiple table joins.

  • True
  • False

Answer: True.

Explanation: AWS Redshift is based on PostgreSQL and supports standard SQL constructsincluding multiple table joins.

Which statement is used to specify a condition while fetching the data?

  • a) SELECT
  • b) JOIN
  • c) WHERE
  • d) ORDER BY

Answer: c) WHERE.

Explanation: The WHERE clause is used to filter records and specify a condition while fetching the data in SQL.

Which SQL keyword is used to sort the result-set?

  • a) WHERE
  • b) ORDER BY
  • c) SORT
  • d) ALIGN

Answer: b) ORDER BY.

Explanation: The ORDER BY keyword is used to sort the result-set in SQL.

True/False: A SQL query can utilize multiple JOIN clauses.

  • True
  • False

Answer: True.

Explanation: A single SQL query can indeed have multiple JOIN clauses, typically when data is being fetched from multiple related tables.

Which JOIN returns all the rows from the left table and the matched rows from the right table. If no match, the result is NULL from the right side?

  • a) INNER JOIN
  • b) LEFT JOIN
  • c) RIGHT JOIN
  • d) FULL JOIN

Answer: b) LEFT JOIN.

Explanation: LEFT JOIN returns all the rows from the left table, and the matched rows from the right table. If there are no matches, the result is NULL on the right side.

Interview Questions

Consider a situation where you have to retrieve data from two tables – ‘Orders’ and ‘Customers’. How can you get information about the orders of each customer using SQL?

You would use a JOIN clause in SQL. Here’s an illustrative statement:

SELECT Customers.CustomerName, Orders.OrderID FROM Customers JOIN Orders ON Customers.CustomerID=Orders.CustomerID;

How to write a SELECT statement in SQL to fetch records with multiple conditions or qualifiers?

We use the WHERE clause to add conditions to a SQL statement. For example,

SELECT * FROM Employees WHERE Salary > 5000 AND Age < 30;

What is the purpose of the "ON" keyword in a SQL query with a JOIN clause?

The "ON" keyword is used to specify the condition for the JOIN. It defines how the tables should be joined, i.e., which columns from each table should be matched.

How would you use the LIKE operator as a qualifier in a SELECT statement?

The LIKE operator is used within a WHERE clause to search for a specified pattern. For instance,

SELECT * FROM Customers WHERE City LIKE 'New%';

This would return all customers whose city starts with 'New'.

Could you write a SQL query using INNER JOIN to display records where 'table1' and 'table2' have common values for the 'common_column'?

Yes, here is an example:

SELECT * from table1 INNER JOIN table2 ON table1.common_column = table2.common_column;

Mention how you would create a named SQL query.

Named SQL queries are defined using the 'AS' keyword. For example,

SELECT column_name AS 'Alias' FROM table_name;

How would you use SQL to display unique records from a table?

You could use the DISTINCT keyword. The syntax would look like this:

SELECT DISTINCT column_name FROM table_name;

How to fetch the top 5 records from a table, say 'Products'?

We can use the LIMIT clause for this purpose. Here is the SQL statement:

SELECT * FROM Products ORDER BY SomeColumn LIMIT 5;

While joining two tables using SQL, which clause would you use to get all the records from one table and matched records from the other?

We would need to use LEFT JOIN for this. The SQL statement would look like:

SELECT * from table1 LEFT JOIN table2 on table1.common_column = table2.common_column;

How can you update a record in a table using SQL?

You can update a record using the UPDATE keyword followed by the SET keyword. Here's an example:

UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition;

Could you explain how to use the UNION operator in a SQL query?

UNION operator is used to combine the result sets of two or more SELECT statements. Each SELECT statement should have the same number of columns and corresponding columns should have compatible data types. For example,

SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;

How can you insert a new record into a table using SQL?

You can use the INSERT INTO statement to insert new data into a table. Syntax:

INSERT INTO table_name (column1, column2..) VALUES (value1, value2..);

How can you delete a specific record from a table using SQL?

You can delete specific records using the DELETE statement. For example,

DELETE FROM table_name WHERE condition;

How to use the BETWEEN operator in SQL?

The BETWEEN operator selects values within a given range inclusive of the end-points. For example,

SELECT column_name FROM table_name WHERE column_name BETWEEN value1 AND value2;

How can you count the number of records in a table using SQL?

You can use the COUNT function to count the number of records. For example,

SELECT COUNT(column_name) FROM table_name;

Leave a Reply

Your email address will not be published. Required fields are marked *