Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

SQL Weekly Quiz

Q1
Identify the Error in Below Sql Code:

SELECT
product_name, price
FROM products
WHERE category = Electronics;

Comment your response:


SQL Weekly Quiz
Q1
Solution

Whenever you filter in SQL you use where condition

Filtering can be on a Numeric Variable or Character Variable

Whenever you put filter on a character Variable , you have to use inverted
Quotes

Here

Category = Electronics is incorrect


It should be Category =“Electronics”

Comment your response:


SQL Weekly Quiz
Q2
Identify the Error in Below Sql Code:

SELECT
product_name
FROM products
WHERE
price > (SELECT AVG(price) FROM products);

Comment your response:


SQL Weekly Quiz
Q2 Solution
The potential issue is related to
how the subquery behaves when the AVG(price) value is NULL. I

f there are no records in the "products" table, the subquery result


will be NULL, and the price > NULL condition will not evaluate as
true for any rows.

To handle the possibility of a NULL result in the subquery, you may


want to use the COALESCE or IS NULL check, depending on your
intention:

Correct Code

SELECT product_name FROM products WHERE price >


COALESCE((SELECT AVG(price) FROM products), 0);

Comment your response:


SQL Weekly Quiz
Q3
Identify the Error in Below Sql Code:

SELECT
employee_name, salary
FROM employees
WHERE salary > 50000
AND department = 'Marketing'
OR department = 'Sales';

Comment your response:


SQL Weekly Quiz
Q3
Solution
In SQL, the AND operator takes precedence over the OR operator.

This means that the query will return rows where the salary is greater than 50000 and
the department is 'Marketing', or it will return rows where the department is 'Sales'
regardless of the salary.
If the intended logic is to retrieve employees with a salary greater than 50000 in either
the 'Marketing' or 'Sales' department, you should use parentheses to explicitly define
the logical grouping:

Correct Code

SELECT employee_name, salary


FROM employees
WHERE salary > 50000
AND (department = 'Marketing' OR department = 'Sales');

Comment your response:


SQL Weekly Quiz
Q4
Identify the Error in Below Sql Code:

SELECT
employees.employee_name,
departments.department_name

FROM employees
LEFT JOIN
departments
ON employees.department_id =departments.id
WHERE location_id = 1;

Comment your response:


SQL Weekly Quiz
Q4
Solution
THE Code is all correct and will run smoothly

Comment your response:


SQL Weekly Quiz
Q5
Identify the Error in Below Sql Code:

SELECT
employees.employee_name,
departments.department_name

FROM employees
LEFT JOIN departments
ON
employees.department_id = departments.id
AND employees.location_id = 2;

Comment your response:


SQL Weekly Quiz
Q5
Solution

In Sql joins,

Use Where to filter not and.

So And should be replaced by Where

Comment your response:


SQL Weekly Quiz
Q6
Choose the Right Option:

SELECT customer_name, order_date


FROM customers
JOIN orders ON
customers.customer_id = orders.customer_id
WHERE order_date > '2022-01-01'
AND order_status = 'Shipped';

What is the Error in the Above Code:


A. The JOIN condition is incorrect.
B. The WHERE conditions are not logically connected.
C. The ORDER BY clause is missing.
D. The SELECT clause is missing an aggregate function.

Comment your response:


SQL Weekly Quiz
Q6
Solutions
B. The WHERE conditions are not logically connected.

The WHERE conditions are connected using the logical AND


operator, which is appropriate for filtering rows based on
multiple conditions. However, the error lies in the potential
logical implication of the conditions. The order_status =
'Shipped' condition might conflict with the order_date > '2022-
01-01' condition.

If an order has an order_date after '2022-01-01', but its


order_status is not 'Shipped', it won't be included in the result. If
you intend for both conditions to be satisfied independently,
then there is no issue. However, if you want to consider orders
that were shipped after '2022-01-01', you might want to use
parentheses to ensure proper logical grouping

Comment your response:


SQL Weekly Quiz
Q7
Choose the Right Option:

SELECT product_name, AVG(price) AS average_price


FROM products
WHERE category = 'Electronics'
AND stock_quantity < 10
GROUP BY product_name;

What is the Error in the Above Code:


A. The GROUP BY clause should include the "price" column.
B. The WHERE conditions are logically incorrect.
C. The AVG(price) alias should be used in the GROUP BY clause.
D. The HAVING clause is missing.

Comment your response:


SQL Weekly Quiz
Q7
Solution

D. The HAVING clause is missing.

In this query, you're using an aggregate function


(AVG(price)) in the SELECT clause and grouping by
product_name. When you want to filter the results
based on the result of an aggregate function, you
should use the HAVING clause.

Comment your response:


SQL Weekly Quiz
Q8
Choose the Right Option:

SELECT employee_name, department_name


FROM employees
LEFT JOIN departments ON employees.department_id
= department.id
WHERE salary > 50000
AND department_name = 'Sales';

What is the Error in the Above Code:


A. The LEFT JOIN condition is incorrect.
B. The WHERE conditions include a mix of unqualified and qualified
column names.
C. The ORDER BY clause is missing.
D. The GROUP BY clause is required.

Comment your response:


SQL Weekly Quiz
Q8
Solutions

B. The WHERE conditions include a mix of unqualified


and qualified column names.

In the WHERE clause, you are using both the


unqualified column name department_name and the
qualified column name salary. To avoid ambiguity and
ensure clarity, it's recommended to consistently use
either qualified or unqualified column names.

Comment your response:


SQL Weekly Quiz
Q9
Choose the Right Option:

SELECT supplier_name, AVG(price) AS average_price


FROM suppliers
JOIN products ON suppliers.supplier_id =
products.supplier_id
WHERE price > 100
GROUP BY supplier_name;

What is the Error in the Above Code:


A. The JOIN condition is incorrect.
B. The GROUP BY clause is missing a column.
C. The HAVING clause is required.
D. The WHERE condition should use the AVG(price) column.

Comment your response:


SQL Weekly Quiz
Q9
Solution

D. The WHERE condition should use the AVG(price)


column.

In this query, you are using the AVG(price) alias in the


SELECT clause to calculate the average price, but in the
WHERE clause, you are trying to filter based on the
price column directly. When filtering on an aggregated
value, you should use the HAVING clause instead.
Here's the corrected code:

Comment your response:


SQL Weekly Quiz
Q10
What is the Error in Below Code

SELECT
department_name, employee_name, salary,

RANK() OVER (PARTITION BY department_name


ORDER BY salary DESC) AS salary_rank

FROM employees
WHERE salary_rank <= 3;

Comment your response:


SQL Weekly Quiz
Q10
Solution
The error in the provided SQL code is due to the incorrect usage of the salary_rank alias in
the WHERE clause. In SQL, you cannot refer to an alias defined in the SELECT clause
directly in the WHERE clause because the WHERE clause is logically processed before the
SELECT clause.
To fix this, you need to use a subquery or a common table expression (CTE) to filter the
results based on the alias. Here's one way to correct the code using a subquery:

SELECT department_name, employee_name, salary, salary_rank


FROM (
SELECT department_name, employee_name, salary,
RANK() OVER (PARTITION BY department_name ORDER BY salary DESC) AS
salary_rank
FROM employees
) ranked_data
WHERE salary_rank <= 3;

Comment your response:

You might also like