Advertisement
Guest User

Database

a guest
Aug 21st, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 6.18 KB | None | 0 0
  1. # Write a SQL solution to output big countries' name, population and area.
  2. select name, population, area
  3. from World
  4. where area > 3000000 or population > 25000000;
  5. --------------------------------------------------------------------------
  6. # Given a table salary, such as the one below, that has m=male and f=female values.
  7. UPDATE salary
  8. SET sex = IF(sex='f','m','f');
  9. --------------------------------------------------------------------------
  10. # Please write a SQL query to output movies with an odd numbered ID and a description that is not 'boring'. Order the result by rating.
  11. select id, movie, description, rating
  12. from cinema
  13. where MOD(id, 2)=1 and description!='boring'
  14. order by rating DESC;
  15. --------------------------------------------------------------------------
  16. # Write a SQL query to find all duplicate emails in a table named Person.
  17.  
  18. select Email
  19. from (
  20.     select Email, count(*) as nums
  21.     from Person
  22.     group by Email) as stat
  23. where nums > 1;
  24. --------------------------------------------------------------------------
  25. # 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:
  26. select FirstName, LastName, City, State
  27. from Person left join Address
  28. on Person.PersonId = Address.PersonId;
  29. --------------------------------------------------------------------------
  30. #  write a SQL query that finds out employees who earn more than their managers
  31. /*
  32. SELECT
  33.     a.Name AS 'Employee'
  34. FROM
  35.     Employee AS a,
  36.     Employee AS b
  37. WHERE
  38.     a.ManagerId = b.Id
  39.         AND a.Salary > b.Salary;*/
  40.  
  41. SELECT
  42.      a.NAME AS Employee
  43. FROM Employee AS a JOIN Employee AS b
  44.      ON a.ManagerId = b.Id
  45.      AND a.Salary > b.Salary
  46. ;
  47. --------------------------------------------------------------------------
  48. # Write a SQL query to find all customers who never order anything.
  49. select Name as 'Customers'
  50. from Customers
  51. where Customers.Id not in (
  52. select CustomerId as 'Id' from Orders
  53. );
  54. --------------------------------------------------------------------------
  55. # Please list out all classes which have more than or equal to 5 students.
  56. select class
  57. from courses
  58. group by class
  59. having count(distinct (student)) >=5;
  60. --------------------------------------------------------------------------
  61. # write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates.
  62. select weather.id as 'Id'
  63. from weather
  64. inner join weather w on DATEDIFF(weather.RecordDate, w.RecordDate) = 1
  65. and weather.Temperature > w.Temperature;
  66. --------------------------------------------------------------------------
  67. # Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.
  68.  
  69. delete p1
  70. from Person p1, Person p2
  71. where p1.Email = p2.Email and p1.Id > p2.Id;
  72. --------------------------------------------------------------------------
  73. # Write a SQL query to get the second highest salary from the Employee table.
  74. select  
  75.     (SELECT DISTINCT
  76.             Salary
  77.         FROM
  78.             Employee
  79.         ORDER BY Salary DESC
  80.         LIMIT 1 OFFSET 1) AS SecondHighestSalary
  81. ;
  82. --------------------------------------------------------------------------
  83. # Mary wants to change seats for the adjacent students.
  84. # Write your MySQL query statement below
  85. SELECT
  86.     (CASE
  87.         WHEN MOD(id, 2) != 0 AND counts != id THEN id + 1
  88.         WHEN MOD(id, 2) != 0 AND counts = id THEN id
  89.         ELSE id - 1
  90.     END) AS id,
  91.     student
  92. from
  93.     seat,
  94.     (SELECT
  95.         COUNT(*) AS counts
  96.     FROM seat) AS seat_counts
  97. ORDER BY id ASC;
  98. --------------------------------------------------------------------------
  99. # Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ranking. Note that after
  100. # a tie, the next ranking number should be the next consecutive integer value. In other words, there should be no "holes"
  101. # between ranks.
  102. SELECT
  103.   Score,
  104.   @rank := @rank + (@prev <> (@prev := Score)) Rank
  105. FROM
  106.   Scores,
  107.   (SELECT @rank := 0, @prev := -1) init
  108. ORDER BY Score desc
  109.  
  110. --------------------------------------------------------------------------
  111. # Write a SQL query to find all numbers that appear at least three times consecutively.
  112.  
  113. SELECT DISTINCT
  114.     l1.Num AS ConsecutiveNums
  115. FROM
  116.     Logs l1,
  117.     Logs l2,
  118.     Logs l3
  119. WHERE
  120.     l1.Id = l2.Id + 1
  121.     AND l2.Id = l3.Id + 1
  122.     AND l1.Num = l2.Num
  123.     AND l2.Num = l3.Num
  124. ;
  125. --------------------------------------------------------------------------
  126. # Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.
  127. SELECT
  128.     Department.name AS 'Department',
  129.     Employee.name AS 'Employee',
  130.     Salary
  131. FROM
  132.     Employee
  133.         JOIN
  134.     Department ON Employee.DepartmentId = Department.Id
  135. WHERE
  136.     (Employee.DepartmentId , Salary) IN
  137.     (   SELECT
  138.             DepartmentId, MAX(Salary)
  139.         FROM
  140.             Employee
  141.         GROUP BY DepartmentId
  142.     )
  143. ;
  144. --------------------------------------------------------------------------
  145. # Write a SQL query to get the nth highest salary from the Employee table.
  146.  
  147. CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
  148. BEGIN
  149. declare M int;
  150. Set M=N-1;
  151.   RETURN (
  152.       # Write your MySQL query statement below.
  153.       select distinct Salary from Employee order by Salary desc limit M, 1
  154.   );
  155. END
  156. --------------------------------------------------------------------------
  157. # Write a SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows.
  158. SELECT
  159.     d.Name AS 'Department', e1.Name AS 'Employee', e1.Salary
  160. FROM
  161.     Employee e1
  162.         JOIN
  163.     Department d ON e1.DepartmentId = d.Id
  164. WHERE
  165.     3 > (SELECT
  166.             COUNT(DISTINCT e2.Salary)
  167.         FROM
  168.             Employee e2
  169.         WHERE
  170.             e2.Salary > e1.Salary
  171.                 AND e1.DepartmentId = e2.DepartmentId
  172.         )
  173. ;
  174. --------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement