日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Leetcode之Database篇

發(fā)布時(shí)間:2023/12/31 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Leetcode之Database篇 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

早晨和陳John一起來實(shí)驗(yàn)室的路上聽他說起leetcode上也有數(shù)據(jù)庫和shell的練習(xí)。于是拿來練練手,發(fā)現(xiàn)數(shù)據(jù)庫的題只有幾道而且做得也很快AC率也蠻高,權(quán)當(dāng)復(fù)習(xí)了一下數(shù)據(jù)庫的基本語法了吧。

1:Employees Earning More Than Their Managers

?

The?Employee?table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

+----+-------+--------+-----------+ | Id | Name | Salary | ManagerId | +----+-------+--------+-----------+ | 1 | Joe | 70000 | 3 | | 2 | Henry | 80000 | 4 | | 3 | Sam | 60000 | NULL | | 4 | Max | 90000 | NULL | +----+-------+--------+-----------+

Given the?Employee?table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

+----------+ | Employee | +----------+ | Joe | +----------+
剛開始沒看清楚,后來研究了一下第一張表才發(fā)現(xiàn)原來ManagerId是屬于Id的,所以用到兩張表,這道題很簡單

SELECT a.NAME FROM Employee a, Employee b WHERE a.ManagerId = b.Id AND a.Salary > b.Salary;

2:Duplicate Emails

Write a SQL query to find all duplicate emails in a table named?Person.

+----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+

For example, your query should return the following for the above table:

+---------+ | Email | +---------+ | a@b.com | +---------+

Note: All emails are in lowercase.

SELECT Email FROM Person GROUP BY Email HAVING COUNT(*)>1

具體關(guān)于group by 和having 的用法參考了別人的一篇博客

http://www.cnblogs.com/gaiyang/archive/2011/04/01/2002452.html

3:Combine Two Tables

Table:?Person

+-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table.

Table:?Address

+-------------+---------+ | Column Name | Type | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ AddressId is the primary key column for this table.

?

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:

FirstName, LastName, City, State

SELECT p.FirstName, p.LastName, a.City, a.State FROM Person p LEFT JOIN Address a USING (PersonId)

主要是兩個(gè)表的連接,參考鏈接如下:

http://www.bkjia.com/Mysql/777046.html

http://www.cnblogs.com/devilmsg/archive/2009/03/24/1420543.html

4:Customers Who Never Order

Suppose that a website contains two tables, the?Customers?table and the?Orders?table. Write a SQL query to find all customers who never order anything.

Table:?Customers.

+----+-------+ | Id | Name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+

Table:?Orders.

+----+------------+ | Id | CustomerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+

Using the above tables as example, return the following:

+-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+

SELECT name FROM Customers c LEFT JOIN Orders o on c.Id = o.CustomerId WHERE o.Id IS NULL

網(wǎng)址http://www.tuicool.com/articles/miAfii給出了三種方法,可供參考

5:Rising Temperature

Given a?Weather?table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.

+---------+------------+------------------+ | Id(INT) | Date(DATE) | Temperature(INT) | +---------+------------+------------------+ | 1 | 2015-01-01 | 10 | | 2 | 2015-01-02 | 25 | | 3 | 2015-01-03 | 20 | | 4 | 2015-01-04 | 30 | +---------+------------+------------------+

For example, return the following Ids for the above Weather table:

+----+ | Id | +----+ | 2 | | 4 | +----+

?首先要內(nèi)聯(lián),其次要注意查詢的ID是哪一張表的ID,w1.Id.

其次有計(jì)算date天數(shù)的函數(shù),課參考http://blog.chinaunix.net/uid-26921272-id-3385920.html

SELECT w1.Id FROM Weather w1 INNER JOIN Weather w2 ON TO_DAYS(w1.Date) = TO_DAYS(w2.Date) + 1 AND w1.Temperature > w2.Temperature

6:Second Highest Salary

Write a SQL query to get the second highest salary from the?Employee?table.

+----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+

For example, given the above Employee table, the second highest salary is?200. If there is no second highest salary, then the query should return?null.

SELECT Max(Salary) FROM Employee WHERE Salary < (SELECT Max(Salary) FROM Employee)

7:明天繼續(xù)

?

轉(zhuǎn)載于:https://www.cnblogs.com/gracyandjohn/p/4562747.html

總結(jié)

以上是生活随笔為你收集整理的Leetcode之Database篇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。