Created
February 8, 2023 00:35
-
-
Save patovala/bef1a505c032146f82f4f08ed736bb68 to your computer and use it in GitHub Desktop.
First Attempt for solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
employees projects | |
+---------------+---------+ +---------------+---------+ | |
| id | int |<----+ +->| id | int | | |
| first_name | varchar | | | | title | varchar | | |
| last_name | varchar | | | | start_date | date | | |
| salary | int | | | | end_date | date | | |
| department_id | int |--+ | | | budget | int | | |
+---------------+---------+ | | | +---------------+---------+ | |
| | | | |
departments | | | employees_projects | |
+---------------+---------+ | | | +---------------+---------+ | |
| id | int |<-+ | +--| project_id | int | | |
| name | varchar | +-----| employee_id | int | | |
+---------------+---------+ +---------------+---------+ | |
*/ | |
SELECT e.first_name, e.last_name, e.salary, | |
d.name as department_name | |
FROM employees AS e | |
JOIN departments AS d ON e.department_id = d.id | |
first_name | last_name | salary | department_name | |
------------+-----------+--------+----------------- | |
Pancho | Villa | 15000 | Reporting | |
John | Smith | 20000 | Reporting | |
Ian | Peterson | 80000 | Engineering | |
Mike | Peterson | 20000 | Engineering | |
Cailin | Ninson | 30000 | Engineering | |
John | Mills | 50000 | Marketing | |
Ava | Muffinson | 10000 | Silly Walks | |
(7 rows) | |
/* seleccionar top two salario por departamento */ | |
SELECT d.name as dept, e.first_name, e.salary | |
FROM ( | |
SELECT id, first_name, department_id, salary, | |
rank() over (partition by department_id order by salary desc) as rank | |
FROM EMPLOYEES order by salary desc, rank asc | |
) e, DEPARTMENTS d | |
WHERE rank <= 2 and e.department_id = d.id | |
dept | first_name | salary | |
-------------+------------+-------- | |
Reporting | Pancho | 15000 | |
Reporting | John | 20000 | |
Engineering | Cailin | 30000 | |
Engineering | Ian | 80000 | |
Marketing | John | 50000 | |
Silly Walks | Ava | 10000 | |
(6 rows) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment