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 

dateFormatting

My solution:
CREATE PROCEDURE dateFormatting() 
BEGIN 
  SELECT DATE(date_str) AS date_iso 
  FROM   documents 
  ORDER  BY id; 
END 

importantEvents

My solution:
CREATE PROCEDURE importantEvents() 
BEGIN 
  SELECT * 
  FROM   events 
  ORDER  BY ( DAYOFWEEK(event_date) + 5 ) % 7, 
            participants DESC; 
END 

validPhoneNumbers

My solution:
CREATE PROCEDURE validPhoneNumbers() 
BEGIN 
  SELECT * 
  FROM   phone_numbers 
  WHERE  phone_number REGEXP "^1-[[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$" 
          OR phone_number REGEXP 
"^[[.left-parenthesis.]]1[[.right-parenthesis.]][[:digit:]]{3}-[[:digit:]]{3}-[[:digit:]]{4}$" 
ORDER  BY surname; 
END