Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dalezhang/employee_test_case
https://github.com/dalezhang/employee_test_case
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/dalezhang/employee_test_case
- Owner: dalezhang
- Created: 2024-07-06T03:33:46.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-07-06T03:58:02.000Z (6 months ago)
- Last Synced: 2024-10-19T17:32:04.856Z (3 months ago)
- Language: JavaScript
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Test case 2: SQL
==================You are given a table in postgres which is a list of employees with their salaries and departments. You need to write a query that will select the person with the maximum salary from each department (or several if the amounts are the same).
You can use dump of table [employee.sql](employee.sql) as a test data, sample schema:
```
postgres=# \d employee
Table "public.employee"
Column | Type | Modifiers
------------+-----------------------+-----------
id | integer | not null
name | character varying(30) |
department | character varying(30) |
salary | integer |
Indexes:
"employee_pkey" PRIMARY KEY, btree (id)
```## Solution:
```sql
SELECT e.department, e.salary, e.id
FROM employee e
JOIN (
SELECT department, MAX(salary) as max_salary
FROM employee
GROUP BY department
) em ON e.department = em.department AND e.salary = em.max_salary ORDER BY e.department;
```