0% found this document useful (0 votes)
4 views

SQL_Solutions_Detailed_Cleaned

The document provides various SQL solutions and explanations, including correct usage of SELECT statements, error corrections, and formatting queries. It covers topics such as displaying employee names, preparing reports, and understanding SQL syntax and functions. Additionally, it addresses common mistakes and best practices in SQL query writing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

SQL_Solutions_Detailed_Cleaned

The document provides various SQL solutions and explanations, including correct usage of SELECT statements, error corrections, and formatting queries. It covers topics such as displaying employee names, preparing reports, and understanding SQL syntax and functions. Additionally, it addresses common mistakes and best practices in SQL query writing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Solutions with Explanations:

1. SELECT Statement for Last Name and Mailing Address:

To return the customer's last name and mailing address:

SQL Query:

SELECT last_name, mailing_address

FROM customers;

Replace 'customers' with the actual table name.

2. Correcting SQL Errors:

a. Corrected Query: SELECT first_name FROM f_staffs;

Error: 'first name' should be 'first_name' (no space).

b. Corrected Query: SELECT first_name || ' ' || last_name AS "DJs on Demand Clients" FROM d_clients;

Error: The concatenation operator is '||', not '|', and 'AS' should be used for aliasing.

c. Corrected Query: SELECT DISTINCT f_order_lines FROM quantity;

Error: 'DISCTINCT' should be 'DISTINCT'.

d. Corrected Query: SELECT order_number FROM f_orders;

Error: 'order number' should be 'order_number'.

3. Displaying Employee Names in Super Star Format:

To display employees "Sue," "Bob," and "Monique" in the format with asterisks:

SQL Query:

SELECT '*** Sue ***' AS "Super Star"


FROM dual

UNION ALL

SELECT '*** Bob ***'

FROM dual

UNION ALL

SELECT '*** Monique ***'

FROM dual;

4. Understanding Query Result for DISTINCT:

Query: SELECT first_name, DISTINCT birthdate FROM f_staffs;

Answer: d. No rows will be returned.

Explanation: DISTINCT cannot be used this way; it applies to entire rows.

5. Preparing a Report with a 5% Raise:

SQL Query:

SELECT last_name,

salary AS "CURRENT SALARY",

salary * 1.05 AS "SALARY WITH 5% RAISE"

FROM f_staffs;

6. Returning Structure of EMPLOYEES Table:

SQL Command: DESCRIBE EMPLOYEES;

Nullable columns can contain NULL values.

7. Report for D_CDs Table:

SQL Query:

SELECT inventory_item, cd_title, music_producer, year_purchased


FROM D_CDs;

8. True/False - SELECT last_name, job_id, salary AS Sal FROM employees;

Answer: True. The query uses a valid alias.

9. True/False - SELECT * FROM job_grades;

Answer: True. This is a valid query.

10. Correcting Coding Errors:

Original Query: SELECT employee_id, last_name sal x 12 ANNUAL SALARY FROM employees;

Corrected Query:

SELECT employee_id,

last_name,

salary * 12 AS "ANNUAL SALARY"

FROM employees;

Errors Fixed:

- 'sal x 12' corrected to 'salary * 12'.

- 'ANNUAL SALARY' properly aliased with 'AS'.

11. Evaluating Arithmetic Expression:

In 'salary * 12 - 400', multiplication is evaluated first.

12. Returning All Columns from f_staffs Table:

Answer: b. *

13. Capability to Choose Columns in SQL:

Answer: b. projection.
14. Column Heading in Query Result:

Query: SELECT last_name AS "Employee"

Answer: c. Employee

15. Largest Value:

Query Producing Largest Value: SELECT salary * (6 + 100);

Answer: b.

16. Formatting Employee Statement:

SQL Query:

SELECT 'Mr./Ms. ' || first_name || ' ' || last_name || ' is an employee of our company.' AS "Employees"

FROM employees;

17. True About SQL Statements:

Answer: c. Keywords cannot be abbreviated or split across lines.

18. Returning Columns with UPPERCASE Headings:

Query: SELECT DEPARTMENT_ID, LAST_NAME, FIRST_NAME FROM employees;

Answer: b.

19. Likely Query Failure:

Query: SELCT * FROM employees;

Answer: a. Typo in 'SELECT'.

20. Saving and Editing SQL Statements:

This involves using the SQL environment to save and edit queries as instructed.

You might also like