Leetcode Database, leetcodedatabase

Source: Internet
Author: User

Leetcode Database, leetcodedatabase

Leetcode Database implemented using oracle analysis functions

Question 1: Department Top Three Salaries

select a.depart, a.name, a.salaryfrom (select d.name depart, e.name name, rank()over(partition by d.id order by e.salary desc) rank, e.salary salaryfrom department d, employee ewhere d.id=e.departmentid) awhere a.rank<=3;

Appendix: Table creation and data preparation

DROP TABLE Employee PURGE;CREATE TABLE Employee (      Id INT PRIMARY KEY,     Name CHAR(20), Salary INT, DepartmentId INT );INSERT INTO Employee(Id, Name,Salary,DepartmentId) VALUES(1,'joe',70000,1);INSERT INTO Employee(Id, Name,Salary,DepartmentId) VALUES(2,'henry',80000,2);INSERT INTO Employee(Id, Name,Salary,DepartmentId) VALUES(3,'sam',60000,2);INSERT INTO Employee(Id, Name,Salary,DepartmentId) VALUES(4,'Max',90000,1);INSERT INTO Employee(Id, Name,Salary,DepartmentId) VALUES(5,'Janet',69000,1);INSERT INTO Employee(Id, Name,Salary,DepartmentId) VALUES(6,'Randy',85000,1);INSERT INTO Employee(Id, Name,Salary,DepartmentId) VALUES(7,'rap',85000,1);DROP TABLE  Department PURGE; CREATE TABLE Department ( Id INT PRIMARY KEY, Name CHAR(20) );INSERT INTO Department(Id, Name) VALUES(1,'IT');INSERT INTO Department(Id, Name) VALUES(2,'Sales');

Question 2: Rank Scores

Write a SQL query to rank scores. If there is a tie between two scores, both shoshould have the same ranking.

Note that after a tie, the next ranking number shocould be the next consecutive integer value.

In other words, there shoshould be no "holes" between rank

select score, dense_rank()over(order by score desc)from Scores ;

Appendix: Table creation and data preparation

DROP TABLE Scores PURGE;CREATE TABLE Scores(    Id INT PRIMARY KEY,    Score FLOAT);INSERT INTO Scores(Id, Score) VALUES(1,3.59);INSERT INTO Scores(Id, Score) VALUES(2,3.65);INSERT INTO Scores(Id, Score) VALUES(3,4.00);INSERT INTO Scores(Id, Score) VALUES(4,3.85);INSERT INTO Scores(Id, Score) VALUES(5,4.00);INSERT INTO Scores(Id, Score) VALUES(6,3.65);



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.