PostgreSQL_Questions_for_Data_Analysts
PostgreSQL_Questions_for_Data_Analysts
Questions
Open-Ended Questions
1. Explain the purpose of the JOIN clause in SQL. Write an example query using an INNER
JOIN to combine data from two tables.
2. Describe how LEFT JOIN differs from RIGHT JOIN in PostgreSQL. When would you use
each one?
3. What does the FULL OUTER JOIN clause do? Provide an example query that demonstrates
its use.
4. How does CROSS JOIN work? Give an example scenario where a CROSS JOIN might be
useful.
5. Write a query using the LPAD function to format a column of numbers to have leading
zeros. Explain why this might be useful.
6. What is a TRIGGER in PostgreSQL, and when would you use it? Write an example of a
trigger that updates a column’s timestamp every time a row is modified.
7. Explain the difference between UNION and UNION ALL. Provide examples of when each
would be appropriate.
8. Describe the GRANT command and its usage. Write an example of how to grant SELECT
permissions on a table to a specific user.
9. How does the REVOKE command work in PostgreSQL? Give an example of revoking a
user’s access to a particular schema.
10. Write a query that uses the UPDATE statement to change a specific value in a table.
Explain when to use WHERE clauses with UPDATE to avoid unintended changes.
11. Describe what happens when the DROP command is used on a table. Is there a way to
recover data after a table is dropped?
12. How does the DELETE statement differ from TRUNCATE? When should you use DELETE
instead of TRUNCATE?
13. Write an example of a query using LIMIT and OFFSET. Explain how these clauses can be
useful for paginating results in a web application.
14. Explain the COUNT function in PostgreSQL. Provide a query that counts the number of
distinct values in a column.
15. What is the purpose of the GROUP BY clause in PostgreSQL? Write an example query
that groups data by a column and calculates an aggregate value.
16. Write a query that finds the average, minimum, and maximum values of a specific
column in a table. Explain how aggregate functions like AVG, MIN, and MAX are used.
17. What is the purpose of indexing in PostgreSQL? Describe how you would create an index
on a table and when you might avoid using indexes.
18. Describe a situation where you would use a subquery in PostgreSQL. Write an example
that shows a subquery in the WHERE clause of a query.
Practical Exam Section
1. Create a new table called departments with the following columns: department_id
(primary key, integer), department_name (text), and location (text). Insert at least three
records into the table.
2. In the employees table, write a query to retrieve all employees who have worked for
more than 5 years, ordered by their starting date.
3. Using the JOIN clause, write a query to find the names of all employees along with the
names of the departments they work in.
4. Write a query to increase the salary of all employees in the salaries table by 10% who
have been with the company for more than 3 years.
5. Create a TRIGGER that automatically inserts a record into an audit_log table every time an
employee's salary is updated.
6. Write a query to delete all records from the salaries table where the salary is below a
certain threshold (e.g., $30,000).
7. Using UNION, combine results from the employees and contractors tables (if available) to
get a list of all personnel in the organization.
8. Grant SELECT and INSERT permissions on the departments table to a new user with the
username analyst_user.
9. Revoke DELETE permissions on the employees table from all users in the public group.
10. Write a query that counts the number of employees in each department and displays the
department name alongside the employee count.
11. Use the OFFSET clause to skip the first 5 records in the salaries table and display the
next 10 records.
12. Create a complex query that calculates the total salary paid in each department and
displays the results in descending order.
13. Create a new index on the employees table for the last_name column to improve search
performance.
14. Create a stored procedure that retrieves all employees in a given department, taking
department_id as a parameter.
15. Create a VIEW called high_earners that shows only employees with a salary above
$80,000.
16. Generate a report with the average salary per department, excluding departments with
fewer than 3 employees.
17. Write a DELETE query that removes all employees who have not updated their contact
information in over 5 years.
18. Perform a DROP operation on the temporary_employees table. Explain what happens
when a table is dropped in PostgreSQL and whether this action can be reversed.