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

PostgreSQL EXAM

The document contains a series of objective and open-ended questions related to PostgreSQL, covering commands, data types, and SQL functions. It also includes practical SQL tasks such as creating tables, inserting data, and writing queries to manipulate and retrieve employee records. Additionally, it addresses permissions and table management commands like GRANT, REVOKE, and DROP.

Uploaded by

Eseoghene
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

PostgreSQL EXAM

The document contains a series of objective and open-ended questions related to PostgreSQL, covering commands, data types, and SQL functions. It also includes practical SQL tasks such as creating tables, inserting data, and writing queries to manipulate and retrieve employee records. Additionally, it addresses permissions and table management commands like GRANT, REVOKE, and DROP.

Uploaded by

Eseoghene
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CODEwithESE

SECTION 1: PostgreSQL Objective Questions


1. Which command is used to retrieve data from a  C) RIGHT JOIN
PostgreSQL table?  D) FULL OUTER JOIN
 A) INSERT 8. Which of the following data types is used to
 B) UPDATE store monetary values in PostgreSQL?
 C) SELECT  A) MONEY
 D) DELETE  B) DECIMAL
2. In PostgreSQL, which data type would be most  C) FLOAT
appropriate to store large amounts of text?  D) INTEGER
 A) VARCHAR 9. Which of the following options is used to create
 B) INTEGER a new table?
 C) BOOLEAN  A) MAKE TABLE
 D) TEXT  B) NEW TABLE
3. What command is used to remove all records  C) CREATE TABLE
from a table without deleting the table itself?  D) INSERT TABLE
 A) DROP 10. What does the GROUP BY clause do in a
 B) DELETE query?
 C) TRUNCATE  A) Sorts the results
 D) REMOVE  B) Limits the number of results
4. Which of the following is NOT a PostgreSQL  C) Groups rows with the same values
constraint?  D) Filters out duplicate rows
 A) UNIQUE 11. Which operator is used for pattern matching
 B) PRIMARY KEY in PostgreSQL?
 C) FOREIGN KEY  A) MATCH
 D) DUPLICATE  B) LIKE
5. What does the LIMIT clause do in a SQL query?  C) PATTERN
 A) Restricts the number of columns  D) EQUAL
returned 12. How do you specify a primary key constraint
 B) Restricts the number of rows returned in PostgreSQL?
 C) Sorts the result in descending order  A) PRIMARY_KEY
 D) Joins tables together  B) PK()
6. What does the COUNT(*) function do?  C) PRIMARY KEY
 A) Counts all rows in a table, including null  D) KEY PRIMARY
values 13. In PostgreSQL, which of the following would
 B) Counts only rows with non-null values allow for automatic sequencing of numbers in a
 C) Returns the total sum of values in a column?
column  A) SERIAL
 D) Counts distinct values only  B) INTEGER
7. Which of the following joins will return all  C) AUTO_INCREMENT
rows when there is a match in either left or right  D) INCREMENT
table? 14. Which of the following aggregate functions
 A) INNER JOIN would you use to find the average of values in a
 B) LEFT JOIN column?
 A) TOTAL Open-Ended Questions
 B) SUM 1. Explain the purpose of the JOIN clause in SQL.
 C) AVG Write an example query using an INNER JOIN to
 D) COUNT combine data from two tables.
15. What is the result of the following query: 2. Describe how LEFT JOIN differs from RIGHT
JOIN in PostgreSQL. When would you use each
SELECT DISTINCT column_name FROM one?
table_name;? 3. What does the FULL OUTER JOIN clause do?
 A) It selects only rows where column_name Provide an example query that demonstrates its use.
is unique 4. How does CROSS JOIN work? Give an example
 B) It returns all rows in table_name scenario where a CROSS JOIN might be useful.
5. Write a query using the LPAD function to format
 C) It removes duplicate values in
a column of numbers to have leading zeros. Explain
column_name why this might be useful.
 D) It renames column_name 6. Write an example of a trigger that updates a
16. To remove duplicates in a query result, which column’s timestamp every time a row is modified.
keyword should be used? 7. Explain the difference between UNION and
 A) UNIQUE JOIN. Provide examples of when each would be
appropriate.
 B) DISTINCT
8. Describe the GRANT command and its usage.
 C) DISTINCTROW Write an example of how to grant SELECT
 D) REMOVE permissions on a table to a specific user.
17. Which of the following commands would you 9. How does the REVOKE command work in
use to update existing data in a table? PostgreSQL? Give an example of revoking a user’s
 A) MODIFY access to a particular schema.
10. Write a query that uses the UPDATE statement
 B) CHANGE
to change a specific value in a table. Explain when
 C) UPDATE to use WHERE clauses with UPDATE to avoid
 D) INSERT unintended changes.
18. What does the following SQL command do: 11. Describe what happens when the DROP
DELETE FROM employees WHERE id = 10? command is used on a table. Is there a way to
 A) Deletes the employees table recover data after a table is dropped?
12. How does the DELETE statement differ from
 B) Deletes the id column
TRUNCATE? When should you use DELETE
 C) Deletes rows where id is 10 instead of TRUNCATE?
 D) Deletes all rows in employees 13. Write an example of a query using LIMIT and
19. Which keyword is used to join multiple OFFSET. Explain how these clauses can be useful
conditions in a WHERE clause? for paginating results in a web application.
 A) OR 14. Explain the COUNT function in PostgreSQL.
Provide a query that counts the number of distinct
 B) AND
values in a column.
 C) EITHER 15. What is the purpose of the GROUP BY clause in
 D) IF PostgreSQL? Write an example query that groups
20. What is a foreign key used for in PostgreSQL? data by a column and calculates an aggregate value.
 A) To uniquely identify each record 16. Write a query that finds the average, minimum,
 B) To enforce a link between data in two and maximum values of a specific column in a table.
Explain how aggregate functions like AVG, MIN,
tables
and MAX are used.
 C) To allow null values in a column 17. Describe a situation where you would use a
 D) To store encrypted data subquery in PostgreSQL. Write an example that
shows a subquery in the WHERE clause of a query.

SECTION 2
Practical Exam Section: Make table corrections where necessary…..
CREATE TABLE Employees (
Employee id serial primary_key,
First_name varcha(50),
Last_name varchar(50),
Department varchar(50),
Position varchar(50),
Salary Numeric (10, 2),
Hire_date DATE,
Manager id INTEGER
);

INSERT INTO employees (first_name, last_name, department, position, salary, hire_date,


manager_id) VALUES
('Alice', 'Johnson', 'Sales', 'Sales Associate', '55000', '2020-03-10', NULL),
('Bob', 'Smith', 'Sales', 'Sales Associate', 52000, '2021-01-15', NULL),
('Carol', 'White', 'Sales', 'Sales Manager', 78000, '2018-09-25', NULL),
('David', 'Brown', 'Engineering', 'Software', 'Engineer', 80000, '2019-04-13', 4),
('Eve', 'Davis', 'Engineering', 'Senior Engineer', 90000, '2017-07-21', NULL),
('Frank', 'Miller', 'Engineering', 'Software Engineer', '75000', '2020-05-30', 5),
('Grace', 'Wilson', 'HR', 'HR Specialist', 60000, '2018-11-11', NULL),
('Henry', 'Taylor', 'HR', 'HR Manager', 85000, '2016-02-15', NULL),
('Ivy', 'Anderson', 'Finance', 'Accountant', ‘70000’, '2019-03-10', NULL),
('Jack', 'Thomas', 'Finance', 'Financial Analyst', 67000, '2021-06-25', NULL);

1. Write an SQL query to retrieve all employee records from the employees table, sorted by hire_date in
descending order.
2. Write an SQL query to select all employees in the "Engineering" department who have a salary above 75,000.
3. Write an SQL query to list the first_name and last_name of all employees hired in 2020.
4. Write an SQL query to find the total number of employees in each department.
5. Write an SQL query to calculate the average salary of employees in the HR department.
6. Write an SQL query to display all unique positions available in the company.
7. Write an SQL command that updates the employee_id uniquely e.g. EMP0001.
8. Write an SQL command to insert a new employee record in the employees table with your own sample values.
9. Write an SQL query to limit the result to only the top 3 highest-paid employees.
10. Write an SQL query to select all employees but skip the first 2 records.
11. Write an SQL command to count the total number of employees who report to a manager.
12. Write an SQL command to add a new column named email to the employees table.
13. Write an SQL command to grant SELECT and INSERT permissions on the employees table to a user named
data_user.
14. Write an SQL command to revoke the INSERT permission on the employees table from the user data_user.
15. Write an SQL command to drop the employees table from the database.

You might also like