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

Answer for Exercise 1 - SQL Fundamentals (SELECT & Filtering Statements)

The document provides SQL exercises focused on basic queries using an 'employees' database. It covers various SQL statements such as SELECT, SELECT DISTINCT, ORDER BY, LIMIT, and conditional statements like WHERE, AND, OR, NOT, and IN. Each exercise includes a question, corresponding SQL query, and expected output for practice.

Uploaded by

plaatjies.ipele
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Answer for Exercise 1 - SQL Fundamentals (SELECT & Filtering Statements)

The document provides SQL exercises focused on basic queries using an 'employees' database. It covers various SQL statements such as SELECT, SELECT DISTINCT, ORDER BY, LIMIT, and conditional statements like WHERE, AND, OR, NOT, and IN. Each exercise includes a question, corresponding SQL query, and expected output for practice.

Uploaded by

plaatjies.ipele
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SQL Exercise: Practicing Basic Queries – Answers

Database: employees_db

Syntax Covered: SELECT statement, SELECT DISTINCT statement, ORDER BY


statement, LIMIT statement, WHERE statement, AND statement, OR statement, NOT
statement, IN statement

Assume you have a table called employees with the following structure:

id first_name last_name department salary hire_date city

1 John Doe IT 55000 2018-06-15 New York

2 Jane Smith HR 48000 2019-07-20 Chicago

3 Mike Johnson Finance 60000 2017-09-30 Los Angeles

4 Sarah Brown IT 53000 2021-03-25 New York

5 David White Marketing 52000 2016-04-10 San Francisco

6 Emily Davis IT 62000 2015-02-14 Chicago

7 Robert Wilson Finance 59000 2019-10-01 Houston

8 Jessica Moore HR 51000 2018-05-22 Los Angeles

9 Daniel Clark Marketing 53000 2022-06-01 Chicago

10 Laura Hall IT 50000 2020-08-10 San Francisco

Here are the definitions for each column in the employees table:

Column Name Definition

id A unique identifier for each employee in the table (Primary Key).

first_name The first name of the employee.

last_name The last name of the employee.

department The department where the employee works (e.g., IT, HR, Finance,
Marketing).

salary The monthly or annual salary of the employee (numeric value).

hire_date The date the employee was hired in the format YYYY-MM-DD.

city The city where the employee is located or works from.


Questions

1. SELECT Statement

Question: Retrieve all columns from the employees table.

SQL Query:

SELECT * FROM employees;

Expected Output (Sample Data):

id first_name last_name department salary hire_date city

1 John Doe IT 55000 2018-06-15 New York

2 Jane Smith HR 48000 2019-07-20 Chicago

3 Mike Johnson Finance 60000 2017-09-30 Los Angeles

4 Sarah Brown IT 53000 2021-03-25 New York

5 David White Marketing 52000 2016-04-10 San Francisco

6 Emily Davis IT 62000 2015-02-14 Chicago

7 Robert Wilson Finance 59000 2019-10-01 Houston

8 Jessica Moore HR 51000 2018-05-22 Los Angeles

9 Daniel Clark Marketing 53000 2022-06-01 Chicago

10 Laura Hall IT 50000 2020-08-10 San Francisco


2. SELECT DISTINCT Statement

Question: Find all the unique departments in the employees table.

SQL Query:

SELECT DISTINCT department FROM employees;

Expected Output:

department

IT

HR

Finance

Marketing

3. ORDER BY Statement

Question: Retrieve all employees' first and last names, ordered by salary in descending
order.

SQL Query:

SELECT first_name,

last_name,

salary

FROM employees

ORDER BY salary DESC;

Expected Output:

first_name last_name salary

Emily Davis 62000

Mike Johnson 60000

Robert Wilson 59000


4. LIMIT Statement

Question: Retrieve the top 5 highest-paid employees.

SQL Query:

SELECT first_name,

last_name,

salary

FROM employees

ORDER BY salary DESC

LIMIT 5;

Expected Output:

first_name last_name salary

Emily Davis 62000

Mike Johnson 60000

Robert Wilson 59000

John Doe 55000

Sarah Brown 53000


5. WHERE Statement

Question: Find employees who work in the IT department.

SQL Query:

SELECT first_name,

last_name,

department

FROM employees

WHERE department = 'IT';

Expected Output:

first_name last_name department

John Doe IT

Sarah Brown IT

6. AND Statement

Question: Find employees who work in the Finance department AND have a salary
greater than 58,000.

SQL Query:

SELECT first_name,

last_name,

department,

salary

FROM employees

WHERE department = 'Finance'

AND salary > 58000;

Expected Output:

first_name last_name department salary

Mike Johnson Finance 60000


7. OR Statement

Question: Find employees who work in the HR department OR the Marketing


department.

SQL Query:

SELECT first_name,

last_name,

department

FROM employees

WHERE department = 'HR' OR department = 'Marketing';

Expected Output:

first_name last_name department

Jane Smith HR

Jessica Moore HR

David White Marketing

8. NOT Statement

Question: Find employees who do not work in the IT department.

SQL Query:

SELECT first_name, last_name, department

FROM employees

WHERE NOT department = 'IT';

Expected Output:

first_name last_name department

Jane Smith HR

Mike Johnson Finance


9. IN Statement

Question: Find employees who are in the HR, IT, or Finance departments.

SQL Query:

SELECT first_name,

last_name,

department

FROM employees

WHERE department IN ('HR', 'IT', 'Finance');

Expected Output:

first_name last_name department

John Doe IT

Jane Smith HR

Robert Wilson Finance


Mike Johnson Finance

10. Combining Conditions

Question: Find employees who are in the IT department, have a salary greater than
50,000, and are located in New York.

SQL Query:

SELECT first_name, last_name, department, salary, city

FROM employees

WHERE department = 'IT'

AND salary > 50000

AND city = 'New York';

Expected Output:

first_name last_name department salary city

John Doe IT 55000 New York


Additional Questions – Combining Multiple SQL Statements

11. Combining WHERE, AND, and ORDER BY

SQL Query:

SELECT first_name,

last_name,

department,

salary

FROM employees

WHERE (department = 'Finance' OR department = 'Marketing')

AND salary > 52000

ORDER BY salary DESC;

12. Combining SELECT DISTINCT, WHERE, and IN

SQL Query:

SELECT DISTINCT city

FROM employees

WHERE department NOT IN ('IT', 'HR');

13. Combining WHERE, NOT, AND, and ORDER BY

SQL Query:

SELECT first_name,

last_name,

department,

salary,

hire_date

FROM employees

WHERE department != 'Finance'


AND salary > 50000

ORDER BY hire_date ASC;

14. Combining WHERE, OR, IN, and LIMIT

SQL Query:

SELECT first_name, last_name, department, city

FROM employees

WHERE city IN ('Chicago', 'Los Angeles')

AND department IN ('IT', 'Marketing')

LIMIT 3;

15. Combining WHERE, AND, OR, NOT, ORDER BY, and LIMIT

SQL Query:

SELECT first_name,

last_name,

department,

salary,

city

FROM employees

WHERE (department = 'IT' OR department = 'Finance')

AND city != 'San Francisco'

AND salary > 55000

ORDER BY salary DESC

LIMIT 5;

You might also like