25 February 2018

placesOfInterestPairs

My solution:
CREATE PROCEDURE placesOfInterestPairs() 
BEGIN 
  SELECT place1, 
         place2 
  FROM   (SELECT sights.name                          AS place1, 
                 sights2.name                         AS place2, 
                 SQRT(POW(sights.x - sights2.x, 2) 
                      + POW(sights.y - sights2.y, 2)) AS distance 
          FROM   sights, 
                 sights AS sights2 
          WHERE  sights.name < sights2.name) AS t 
  WHERE  distance < 5 
  ORDER  BY place1, 
            place2; 
END 

userCountries

My solution:
CREATE PROCEDURE userCountries() 
BEGIN 
  SELECT id, 
         country 
  FROM   users, 
         cities 
  WHERE  users.city = cities.city 
  UNION 
  SELECT DISTINCT id, 
                  "unknown" AS country 
  FROM   users, 
         cities 
  WHERE  users.city NOT IN (SELECT city 
                            FROM   cities) 
  ORDER  BY id; 
END 

scholarshipsDistribution

My solution:
CREATE PROCEDURE scholarshipsDistribution() 
BEGIN 
  SELECT candidate_id AS student_id 
  FROM   candidates 
  WHERE  candidate_id NOT IN (SELECT DISTINCT candidate_id AS id 
                              FROM   candidates, 
                                     detentions 
                              WHERE  candidate_id = student_id 
                              ORDER  BY candidate_id); 
END 

24 February 2018

companyEmployees

My solution:
CREATE PROCEDURE companyEmployees() 
BEGIN 
  SELECT dep_name, 
         emp_name 
  FROM   departments, 
         employees 
  ORDER  BY dep_name, 
            emp_name; 
END 

alarmClocks

My solution:
CREATE PROCEDURE alarmClocks() 
BEGIN 
  DROP TABLE IF EXISTS calendarWeek; 

  CREATE TABLE calendarWeek 
    ( 
       `number` INT 
    ); 

  SET @s = 0; 

  loopLabel: 
LOOP 
    INSERT INTO calendarWeek 
                (`number`) 
    VALUES      (@s); 
    SET @s = @s + 1; 
    IF @s <= 53 THEN 
      ITERATE loopLabel; 
    END IF; 
    LEAVE loopLabel; 
  END LOOP loopLabel; 

  SELECT alarm_date 
  FROM   (SELECT DATE_ADD(input_date, INTERVAL (7 * number) DAY) AS alarm_date 
          FROM   userInput, 
                 calendarWeek) AS t, 
         userInput 
  WHERE  YEAR(alarm_date) = YEAR(input_date); 
END 

netIncome

My solution:
CREATE PROCEDURE 
  netIncome() 
BEGIN 
  SELECT   YEAR(   date)                                                AS year, 
           QUARTER(date)                                                AS quarter, 
           SUM(CONVERT(profit, SIGNED INT) - CONVERT(loss, SIGNED INT)) AS net_profit 
  FROM     accounting 
  GROUP BY year, 
           quarter 
  ORDER BY date; 

END

Note:
profit: BIGINT UNSIGNED and
loss: BIGINT UNSIGNED
need to be converted to SIGNED INT.

21 February 2018

pastEvents

My solution:
CREATE PROCEDURE pastEvents() 
BEGIN 
  SELECT name, 
         event_date 
  FROM   Events, 
         (SELECT MAX(DATE(event_date)) AS last_date 
          FROM   Events) AS t 
  WHERE  event_date != t.last_date 
         AND DATEDIFF(t.last_date, event_date) <= 7 
  ORDER  BY event_date DESC; 
END