PostgreSQL Basic SELECT Statement: Display name, salary and 15% of salary as PF for all employees
4. Write a query to get the names (first_name, last_name), salary and 15% of salary as PF for all the employees.
Sample Solution:
Code:
-- This SQL query selects the 'first_name', 'last_name', 'salary', and calculates 15% of the salary as 'PF' (Provident Fund) from the 'employees' table.
SELECT first_name, last_name, salary, salary*.15 PF
FROM employees;
Explanation:
- The SELECT statement is used to retrieve data from a database table.
- first_name, last_name, and salary are the names of the columns being selected from the employees table.
- salary*.15 PF calculates 15% of the salary for each employee and assigns it to the alias 'PF'.
- This query retrieves the first name, last name, salary, and calculates the PF (Provident Fund) amount for each employee from the 'employees' table.
Sample table: employees
Output:
pg_exercises=# SELECT first_name, last_name, salary, salary*.15 PF pg_exercises-# FROM employees; first_name | last_name | salary | pf -------------+-------------+----------+----------- Steven | King | 24000.00 | 3600.0000 Neena | Kochhar | 17000.00 | 2550.0000 Lex | De Haan | 17000.00 | 2550.0000 Alexander | Hunold | 9000.00 | 1350.0000 Bruce | Ernst | 6000.00 | 900.0000 .... Jose Manuel | Urman | 7800.00 | 1170.0000 Luis | Popp | 6900.00 | 1035.0000 Den | Raphaely | 11000.00 | 1650.0000 Alexander | Khoo | 3100.00 | 465.0000 Shelli | Baida | 2900.00 | 435.0000 Sigal | Tobias | 2800.00 | 420.0000 .... Julia | Nayer | 3200.00 | 480.0000 Irene | Mikkilineni | 2700.00 | 405.0000 James | Landry | 2400.00 | 360.0000 Steven | Markle | 2200.00 | 330.0000 .... Jason | Mallin | 3300.00 | 495.0000 Michael | Rogers | 2900.00 | 435.0000 Ki | Gee | 2400.00 | 360.0000 Hazel | Philtanker | 2200.00 | 330.0000 Renske | Ladwig | 3600.00 | 540.0000 Stephen | Stiles | 3200.00 | 480.0000 John | Seo | 2700.00 | 405.0000 Joshua | Patel | 2500.00 | 375.0000 Trenna | Rajs | 3500.00 | 525.0000 ....
Practice Online
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a query to get the details of all employees from the employee table in descending order by their first name.
Next: Write a query to get the employee ID, name (first_name, last_name) and salary in ascending order according to their salary.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics