Skip to content

Commit 962e897

Browse files
authored
Add files via upload
1 parent 6dd3169 commit 962e897

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
-- Question 78
2+
-- Table Variables:
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | name | varchar |
8+
-- | value | int |
9+
-- +---------------+---------+
10+
-- name is the primary key for this table.
11+
-- This table contains the stored variables and their values.
12+
13+
14+
-- Table Expressions:
15+
16+
-- +---------------+---------+
17+
-- | Column Name | Type |
18+
-- +---------------+---------+
19+
-- | left_operand | varchar |
20+
-- | operator | enum |
21+
-- | right_operand | varchar |
22+
-- +---------------+---------+
23+
-- (left_operand, operator, right_operand) is the primary key for this table.
24+
-- This table contains a boolean expression that should be evaluated.
25+
-- operator is an enum that takes one of the values ('<', '>', '=')
26+
-- The values of left_operand and right_operand are guaranteed to be in the Variables table.
27+
28+
29+
-- Write an SQL query to evaluate the boolean expressions in Expressions table.
30+
31+
-- Return the result table in any order.
32+
33+
-- The query result format is in the following example.
34+
35+
-- Variables table:
36+
-- +------+-------+
37+
-- | name | value |
38+
-- +------+-------+
39+
-- | x | 66 |
40+
-- | y | 77 |
41+
-- +------+-------+
42+
43+
-- Expressions table:
44+
-- +--------------+----------+---------------+
45+
-- | left_operand | operator | right_operand |
46+
-- +--------------+----------+---------------+
47+
-- | x | > | y |
48+
-- | x | < | y |
49+
-- | x | = | y |
50+
-- | y | > | x |
51+
-- | y | < | x |
52+
-- | x | = | x |
53+
-- +--------------+----------+---------------+
54+
55+
-- Result table:
56+
-- +--------------+----------+---------------+-------+
57+
-- | left_operand | operator | right_operand | value |
58+
-- +--------------+----------+---------------+-------+
59+
-- | x | > | y | false |
60+
-- | x | < | y | true |
61+
-- | x | = | y | false |
62+
-- | y | > | x | true |
63+
-- | y | < | x | false |
64+
-- | x | = | x | true |
65+
-- +--------------+----------+---------------+-------+
66+
-- As shown, you need find the value of each boolean exprssion in the table using the variables table.
67+
68+
-- Solution
69+
with t1 as(
70+
select e.left_operand, e.operator, e.right_operand, v.value as left_val, v_1.value as right_val
71+
from expressions e
72+
join variables v
73+
on v.name = e.left_operand
74+
join variables v_1
75+
on v_1.name = e.right_operand)
76+
77+
select t1.left_operand, t1.operator, t1.right_operand,
78+
case when t1.operator = '<' then (select t1.left_val< t1.right_val)
79+
when t1.operator = '>' then (select t1.left_val > t1.right_val)
80+
when t1.operator = '=' then (select t1.left_val = t1.right_val)
81+
else FALSE
82+
END AS VALUE
83+
from t1
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- Question 80
2+
-- Table: Logs
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | log_id | int |
8+
-- +---------------+---------+
9+
-- id is the primary key for this table.
10+
-- Each row of this table contains the ID in a log Table.
11+
12+
-- Since some IDs have been removed from Logs. Write an SQL query to find the start and end number of continuous ranges in table Logs.
13+
14+
-- Order the result table by start_id.
15+
16+
-- The query result format is in the following example:
17+
18+
-- Logs table:
19+
-- +------------+
20+
-- | log_id |
21+
-- +------------+
22+
-- | 1 |
23+
-- | 2 |
24+
-- | 3 |
25+
-- | 7 |
26+
-- | 8 |
27+
-- | 10 |
28+
-- +------------+
29+
30+
-- Result table:
31+
-- +------------+--------------+
32+
-- | start_id | end_id |
33+
-- +------------+--------------+
34+
-- | 1 | 3 |
35+
-- | 7 | 8 |
36+
-- | 10 | 10 |
37+
-- +------------+--------------+
38+
-- The result table should contain all ranges in table Logs.
39+
-- From 1 to 3 is contained in the table.
40+
-- From 4 to 6 is missing in the table
41+
-- From 7 to 8 is contained in the table.
42+
-- Number 9 is missing in the table.
43+
-- Number 10 is contained in the table.
44+
45+
-- Solution
46+
select min(log_id) as start_id, max(log_id) as end_id
47+
from(
48+
select log_id, log_id-row_number() over (order by log_id) as rk
49+
from logs) a
50+
group by rk

Medium/Rectangles Area.sql

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- Question 79
2+
-- Table: Points
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | id | int |
8+
-- | x_value | int |
9+
-- | y_value | int |
10+
-- +---------------+---------+
11+
-- id is the primary key for this table.
12+
-- Each point is represented as a 2D Dimensional (x_value, y_value).
13+
-- Write an SQL query to report of all possible rectangles which can be formed by any two points of the table.
14+
15+
-- Each row in the result contains three columns (p1, p2, area) where:
16+
17+
-- p1 and p2 are the id of two opposite corners of a rectangle and p1 < p2.
18+
-- Area of this rectangle is represented by the column area.
19+
-- Report the query in descending order by area in case of tie in ascending order by p1 and p2.
20+
21+
-- Points table:
22+
-- +----------+-------------+-------------+
23+
-- | id | x_value | y_value |
24+
-- +----------+-------------+-------------+
25+
-- | 1 | 2 | 8 |
26+
-- | 2 | 4 | 7 |
27+
-- | 3 | 2 | 10 |
28+
-- +----------+-------------+-------------+
29+
30+
-- Result table:
31+
-- +----------+-------------+-------------+
32+
-- | p1 | p2 | area |
33+
-- +----------+-------------+-------------+
34+
-- | 2 | 3 | 6 |
35+
-- | 1 | 2 | 2 |
36+
-- +----------+-------------+-------------+
37+
38+
-- p1 should be less than p2 and area greater than 0.
39+
-- p1 = 1 and p2 = 2, has an area equal to |2-4| * |8-7| = 2.
40+
-- p1 = 2 and p2 = 3, has an area equal to |4-2| * |7-10| = 6.
41+
-- p1 = 1 and p2 = 3 It's not possible because the rectangle has an area equal to 0.
42+
43+
-- Solution
44+
select p1.id as p1, p2.id as p2, abs(p1.x_value-p2.x_value)*abs(p1.y_value-p2.y_value) as area
45+
from points p1 cross join points p2
46+
where p1.x_value!=p2.x_value and p1.y_value!=p2.y_value and p1.id<p2.id
47+
order by area desc, p1, p2

0 commit comments

Comments
 (0)