hackerrank刷题总结
1.You are given a table, Projects, containing three columns: Task_ID, Start_Date and End_Date. It is guaranteed that the difference between the End_Date and the Start_Date is equal to 1 day for each row in the table.
If the End_Date of the tasks are consecutive, then they are part of the same project. Samantha is interested in finding the total number of different projects completed.
Write a query to output the start and end dates of projects listed by the number of days it took to complete the project in ascending order. If there is more than one project that have the same number of completion days, then order by the start date of the project.
答案:
SET sql_mode = '';
SELECT Start_Date, End_Date
FROM
(SELECT Start_Date FROM Projects WHERE Start_Date NOT IN (SELECT End_Date FROM Projects)) a,
(SELECT End_Date FROM Projects WHERE End_Date NOT IN (SELECT Start_Date FROM Projects)) b
WHERE Start_Date < End_Date
GROUP BY Start_Date
ORDER BY DATEDIFF(End_Date, Start_Date), Start_Date;
分析:参考讨论区大佬的答案,是mysql。首先通过样例表发现,只要开始日期不等于结束日期,说明上一个结束日期不是这个的开始日期也就是说这不是一个连续的,不是一个项目。同理还要保证结束日期不是开始日期。并以开始日期作为分类标准,用datediff(数据1,数据2)返回两个日期之间的参数。set sql_mode='':它定义了你MySQL应该支持的sql语法,对数据的校验等等
2.You are given three tables: Students, Friends and Packages. Students contains two columns: ID and Name. Friends contains two columns: ID and Friend_ID (ID of the ONLY best friend). Packages contains two columns: ID and Salary (offered salary in $ thousands per month).
Write a query to output the names of those students whose best friends got offered a higher salary than them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no two students got same salary offer.
答案:
SELECT s.NAME
FROM Students s JOIN Friends f ON s.ID=f.ID JOIN Packages p1 ON s.ID=p1.ID JOIN Packages p2 ON f.Friend_ID=p2.ID
WHERE p2.Salary>p1.Salary
ORDER BY p2.Salary;
分析:学生的id与朋友表的id是关联的,学生的id与工资表的id是关联的,朋友表的id与工资表的id是关联的,所以选择三表关联,与学生表关联的工资小于与朋友表关联的工资。
3.
Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A)
, ADoctorName(D)
, AProfessorName(P)
, and ASingerName(S)
.
Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
答案:
SELECT CONCAT(Name,'(',SUBSTR(Occupation,1,1),')')
FROM OCCUPATIONS
ORDER BY Name;
SELECT CONCAT('There are a total of ',COUNT(Occupation),' ',LOWER(Occupation),'s.')
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY COUNT(Occupation),Occupation;
解析:concat函数是将多个字符串连成一个字符串。substr是字符串的截取:substr(列名,开始点,长度)截取指定范围长度的子字符串
4. Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
- Equilateral: It's a triangle with sides of equal length.
- Isosceles: It's a triangle with sides of equal length.
- Scalene: It's a triangle with sides of differing lengths.
- Not A Triangle: The given values of A, B, and C don't form a triangle
答案:
SELECT CASE WHEN A+B<=C OR A+C<=B OR B+C<=A THEN 'Not A Triangle'
WHEN A=B AND B=C THEN 'Equilateral'
WHEN A=B OR A=C OR B=C THEN 'Isosceles'
ELSE 'Scalene'
END
FROM TRIANGLES;
解析:使用case搜索函数