반응형
Task
Given a table where users' connections are logged, find the first and the last address of the networks they connected from.
Notes
- Order the result by the id column
- There's no need to validate anything - it's okay if the user connects from a private network
- (You don't need the connection_time field for this task but without it the input data looks too dull)
- You can read more about IPv4 on Wikipedia (check the First and last subnet addresses section if you need an example/explanation related to this task only)
Input table
---------------------------------------------
| Table | Column | Type |
|-------------+-----------------+-----------|
| connections | id | int |
| | connection_time | timestamp |
| | ip_address | inet |
---------------------------------------------
Output table
------------------------
| Column | Type |
|---------------+------|
| id | int |
| first_address | text |
| last_address | text |
------------------------
Example
For the IP address 182.240.42.115/24 the first address in the network is 182.240.42.0/24, and the last one is 182.240.42.255/24.
Solution:
SELECT
id,
NETWORK(ip_address) AS first_address,
BROADCAST(ip_address) AS last_address
FROM connections
ORDER BY id
broadcast(inet) is broadcast address for network -> 192.168.1.255/24
network(inet) is extract network part of address -> 192.168.1.0/24
Reference:
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
Easy SQL: Counting and Grouping (0) | 2022.06.29 |
---|---|
Countries Capitals for Trivia Night (SQL for Beginners #6) (0) | 2022.06.28 |
SQL Basics - Trimming the Field (0) | 2022.06.26 |
SQL: Disorder (0) | 2022.06.25 |
Adults only (SQL for Beginners #1) (0) | 2022.06.25 |