w3resource logo


PostgreSQL exercises

PostgreSQL Restricting and Sorting Data - Exercises, Practice, Solution

Secondary Nav

Simple select statements [11 exercises with solution]

1. Write a query to display the names (first_name, last_name) and salary for all employees whose salary is not in the range $10,000 through $15,000.

Go to editor

SELECT first_name, last_name, salary 
FROM employees 
WHERE salary NOT BETWEEN 10000 AND 15000;

Sample table : employees

2. Write a query to display the names (first_name, last_name) and department ID of all employees in departments 30 or 100 in ascending alphabetical order by department ID.

Go to editor

SELECT first_name, last_name, department_id 
FROM employees 
WHERE department_id IN (30, 100) 
ORDER BY  department_id  ASC; 

Sample table : employees

3. Write a query to display the names (first_name, last_name) and salary for all employees whose salary is not in the range $10,000 through $15,000 and are in department 30 or 100.

Go to editor

SELECT first_name, last_name, salary, department_id 
FROM employees 
WHERE salary NOT BETWEEN 10000 AND 15000 
AND department_id IN (30, 100);

Sample table : employees

4. Write a query to display the names (first_name, last_name) and hire date for all employees who were hired in 1987.

Go to editor

SELECT first_name, last_name, hire_date 
FROM employees 
WHERE TO_CHAR(hire_date, 'YYYY')  LIKE '%87';

Sample table : employees

5. Write a query to display the first_name of all employees who have both an "c" and "e" in their first name.

Go to editor

SELECT first_name 
FROM employees 
WHERE first_name LIKE '%c%' 
AND first_name LIKE '%e%';

Sample table : employees

6. Write a query to display the last name, job, and salary for all employees whose job is that of a Programmer or a Shipping Clerk, and whose salary is not equal to $4,500, $10,000, or $15,000.

Go to editor

SELECT last_name, job_id, salary 
FROM employees 
WHERE job_id IN ('IT_PROG', 'SH_CLERK') 
AND salary NOT IN (4500,10000, 15000); 

Sample table : employees

7. Write a query to display the last names of employees whose names have exactly 6 characters.

Go to editor

SELECT last_name 
FROM employees 
WHERE last_name LIKE '______';

Sample table : employees

8. Write a query to display the last names of employees having 'e' as the third character.

Go to editor

SELECT last_name 
FROM employees 
WHERE last_name LIKE '__e%';

Sample table : employees

9. Write a query to display the jobs/designations available in the employees table.

Go to editor

Sample table : employees

10. Write a query to display the names (first_name, last_name), salary and PF (15% of salary) of all employees.

Go to editor

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

Sample table : employees

11. Write a query to select all record from empployees where last name in 'BLAKE', 'SCOTT', 'KING' and 'FORD'.

Go to editor

SELECT * 
FROM employees 
WHERE last_name IN('Jones', 'Blake', 'Scott', 'King', 'Ford');

Sample table : employees

 

... More

Structure of 'hr' database :

hr database

Practice Online


Go To Top