[SQL] Ranking
先建立一個練習表格:
CREATE TABLE Department ( id SERIAL PRIMARY KEY, name VARCHAR NOT NULL ); CREATE TABLE Employee ( id SERIAL PRIMARY KEY, name VARCHAR NOT NULL, salary INT, dep_id INT, ); INSERT INTO Employee(name, salary, dep_id) VALUES ('Mandy', 45000, 2); INSERT INTO Employee(name, salary, dep_id) VALUES ('Emily', 43000, 1); INSERT INTO Employee(name, salary, dep_id) VALUES ('Sylvia', 26000, 2); INSERT INTO Employee(name, salary, dep_id) VALUES ('Eva', 48000, 3); INSERT INTO Employee(name, salary, dep_id) VALUES ('Sandra', 33000, 3); INSERT INTO Employee(name, salary, dep_id) VALUES ('Lily', 28000, 2); INSERT INTO Employee(name, salary, dep_id) VALUES ('April', 50000, 1); INSERT INTO Employee(name, salary, dep_id) VALUES ('Cindy', 43000, 1); INSERT INTO Employee(name, salary, dep_id) VALUES ('Kay', 40000, 3); INSERT INTO Department(name) VALUES ('IT'); INSERT INTO Department(name) VALUES ('RD'); INSERT INTO Department(name) VALUES ('QA'); 資料排序 直接用 ORDER BY 無法得到秩(排名)