w3resource logo


PostgreSQL exercises

PostgreSQL basic SELECT statement - Exercises, Practice, Solution

Secondary Nav

Basic SELECT statement [19 exercises with solution]

1. Write a query to display the names (first_name, last_name) using alias name “First Name", "Last Name".

Go to editor

SELECT first_name "First Name",  last_name "Last Name" 
FROM employees;

Sample table : employees

2. Write a query to get unique department ID from employee table.

Go to editor

Sample table : employees

3. Write a query to get all employee details from the employee table order by first name, descending.

Go to editor

Sample table : employees

4. Write a query to get the names (first_name, last_name), salary, PF of all the employees (PF is calculated as 12% of salary).

Go to editor

SELECT first_name, last_name, salary, salary*.15 PF 
FROM employees;

Sample table : employees

5. Write a query to get the employee ID, names (first_name, last_name), salary in ascending order of salary.

Go to editor

SELECT employee_id, first_name, last_name, salary 
FROM employees 
ORDER BY salary;

Sample table : employees

6. Write a query to get the total salaries payable to employees.

Go to editor

Sample table : employees

7. Write a query to get the maximum and minimum salary from employees table.

Go to editor

Sample table : employees

8. Write a query to get the average salary and number of employees in the employees table.

Go to editor

Sample table : employees

9. Write a query to get the number of employees working with the company.

Go to editor

Sample table : employees

10. Write a query to get the number of jobs available in the employees table.

Go to editor

Sample table : employees

11. Write a query get all first name from employees table in upper case.

Go to editor

Sample table : employees

12. Write a query to get the first 3 characters of first name from employees table.

Go to editor

Sample table : employees

13. Write a query to calculate 171*214+625.

Go to editor

14. Write a query to get the names (for example Ellen Abel, Sundar Ande etc.) of all the employees from employees table.

Go to editor

SELECT  CONCAT(first_name,' ', last_name) "Employee Name" 
FROM employees;

Sample table : employees

15. Write a query to get first name from employees table after removing white spaces from both side.

Go to editor

Sample table : employees

16. Write a query to get the length of of the employee names (first_name, last_name) from employees table.

Go to editor

SELECT first_name,last_name, 
LENGTH(first_name)+LENGTH(last_name)  "Length of  Names" 
FROM employees;

17. Write a query to check if the first_name fields of the employees table contains numbers.

Go to editor

SELECT * 
FROM employees 
WHERE  first_name 
SIMILAR TO   '%0|1|2|3|4|5|6|7|8|9%';

18. Write a query to select first 10 records from a table.

Go to editor

SELECT employee_id, first_name 
FROM employees  LIMIT 10;

Sample table : employees

19. Write a query to get monthly salary (round 2 decimal places) of each and every employee?
Note : Assume the salary field provides the 'annual salary' information.

Go to editor

SELECT first_name, last_name, 
ROUND(salary/12,2) as "Monthly Salary" 
FROM employees;

Sample table : employees

 

... More

Structure of 'hr' database :

hr database

Practice Online


Go To Top