/*
Amazon SQL Interview Question 

You are given the table with titles of recipes from a cookbook and their page numbers. You are asked to represent how the recipes will be distributed in the book.

Produce a table consisting of three columns: left_page_number, left_title and right_title. The k-th row (counting from 0), should contain the number and the title of the page with the number 2×k in the first and second columns respectively, and the title of the page with the number 2×k+1 in the third column.

Each page contains at most 1 recipe. If the page does not contain a recipe, the appropriate cell should remain empty (NULL value). Page 0 (the internal side of the front cover) is guaranteed to be empty.
*/
    
-- Schema Setup
CREATE TABLE cookbook_titles (page_number INT PRIMARY KEY,title VARCHAR(255));

INSERT INTO cookbook_titles (page_number, title) VALUES (1, 'Scrambled eggs'), (2, 'Fondue'), (3, 'Sandwich'), (4, 'Tomato soup'), (6, 'Liver'), (11, 'Fried duck'), (12, 'Boiled duck'), (15, 'Baked chicken');

-- Solution
WITH left_pages AS
(
 SELECT
     page_number,
     title
 FROM cookbook_titles
 WHERE MOD(page_number,2) = 0
)
,right_pages AS
(
 SELECT
     page_number,
     title
 FROM cookbook_titles
 WHERE MOD(page_number,2) <> 0
)
    
SELECT
    l.page_number AS left_page_number,
    l.title AS left_title,
    r.title AS right_title
FROM left_pages l
LEFT JOIN right_pages r
ON r.page_number = l.page_number + 1;

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: