Professional Documents
Culture Documents
SQL QUIZ1 QuestionandAnswers
SQL QUIZ1 QuestionandAnswers
Q1
Identify the Error in Below Sql Code:
SELECT
product_name, price
FROM products
WHERE category = Electronics;
Whenever you put filter on a character Variable , you have to use inverted
Quotes
Here
SELECT
product_name
FROM products
WHERE
price > (SELECT AVG(price) FROM products);
Correct Code
SELECT
employee_name, salary
FROM employees
WHERE salary > 50000
AND department = 'Marketing'
OR department = 'Sales';
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
employees.employee_name,
departments.department_name
FROM employees
LEFT JOIN
departments
ON employees.department_id =departments.id
WHERE location_id = 1;
SELECT
employees.employee_name,
departments.department_name
FROM employees
LEFT JOIN departments
ON
employees.department_id = departments.id
AND employees.location_id = 2;
In Sql joins,
SELECT
department_name, employee_name, salary,
FROM employees
WHERE salary_rank <= 3;