π 08/08/2024
π
Given the CITY and COUNTRY tables, query the names of all the continents (COUNTRY.Continent) and their respective average city populations (CITY.Population) rounded down to the nearest integer.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
The CITY and COUNTRY tables are described as follows:
CITY:
Field | Type |
---|---|
ID | NUMBER |
NAME | VARCHAR2(17) |
COUNTRYCODE | VARCHAR2(3) |
DISTRICT | VARCHAR2(20) |
POPULATION | NUMBER |
COUNTRY:
Field | Type |
---|---|
CODE | VARCHAR2(3) |
NAME | VARCHAR2(44) |
CONTINENT | VARCHAR2(13) |
REGION | VARCHAR2(25) |
SURFACEAREA | NUMBER |
INDEPYEAR | VARCHAR2(2) |
POPULATION | NUMBER |
LIFEEXPECTANCY | VARCHAR2(4) |
GNP | NUMBER |
GNPOLD | VARCHAR2(9) |
LOCALNAME | VARCHAR2(44) |
GOVERNMENTFORM | VARCHAR2(44) |
HEADOFSTATE | VARCHAR2(32) |
CAPITAL | VARCHAR2(4) |
CODE2 | VARCHAR2(2) |
SELECT
co.Continent,
FLOOR(AVG(ci.Population)) AS AvgPopulation
FROM
CITY ci
JOIN
COUNTRY co ON ci.CountryCode = co.Code
GROUP BY
co.Continent;
π 08/08/2024
π
Consider P1(a, b) and P2(c, d) to be two points on a 2D plane.
- a happens to equal the minimum value in Northern Latitude (LAT_N in STATION).
- b happens to equal the minimum value in Western Longitude (LONG_W in STATION).
- c happens to equal the maximum value in Northern Latitude (LAT_N in STATION).
- d happens to equal the maximum value in Western Longitude (LONG_W in STATION).
Query the Manhattan Distance between points and and round it to a scale of decimal places.
The STATION table is described as follows:
Field | Type |
---|---|
ID | NUMBER |
CITY | VARCHAR2(21) |
STATE | VARCHAR2(2) |
LAT_N | NUMBER |
LONG_W | NUMBER |
where LAT_N is the northern latitude and LONG_W is the western longitude.
SELECT
ROUND(
ABS(MIN(LAT_N) - MAX(LAT_N)) + ABS(MIN(LONG_W) - MAX(LONG_W)),
4
) AS manhattan_distance
FROM
STATION;
π 08/08/2024
π
Amber's conglomerate corporation just acquired some new companies. Each of the companies follows this hierarchy:
Founder
|/
|
Lead Manager
|/
|
Senior Manager
|/
|
Manager
|/
|
Employee
Given the table schemas below, write a query to print the company_code, founder name, total number of lead managers, total number of senior managers, total number of managers, and total number of employees. Order your output by ascending company_code.
Note:
- The tables may contain duplicate records.
- The company_code is string, so the sorting should not be numeric. For example, if the company_codes are C_1, C_2, and C_10, then the ascending company_codes will be C_1, C_10, and C_2.
The following tables contain company data:
- Company: The company_code is the code of the company and founder is the founder of the company.
Column | Type |
---|---|
company_code | String |
founder | String |
- Lead_Manager: The lead_manager_code is the code of the lead manager, and the company_code is the code of the working company.
Column | Type |
---|---|
lead_manager_code | String |
company_code | String |
- Senior_Manager: The senior_manager_code is the code of the senior manager, the lead_manager_code is the code of its lead manager, and the company_code is the code of the working company.
Column | Type |
---|---|
senior_manager_code | String |
lead_manager_code | String |
company_code | String |
- Manager: The manager_code is the code of the manager, the senior_manager_code is the code of its senior manager, the lead_manager_code is the code of its lead manager, and the company_code is the code of the working company.
Column | Type |
---|---|
manager_code | String |
senior_manager_code | String |
lead_manager_code | String |
company_code | String |
- Employee: The employee_code is the code of the employee, the manager_code is the code of its manager, the senior_manager_code is the code of its senior manager, the lead_manager_code is the code of its lead manager, and the company_code is the code of the working company.
Column | Type |
---|---|
employee_code | String |
manager_code | String |
senior_manager_code | String |
lead_manager_code | String |
company_code | String |
Company Table:
company_code | founder |
---|---|
C1 | Monika |
C2 | Samantha |
Lead_Manager Table:
lead_manager_code | company_code |
---|---|
LM1 | C1 |
LM2 | C2 |
Senior_Manager Table:
senior_manager_code | lead_manager_code | company_code |
---|---|---|
SM1 | LM1 | C1 |
SM2 | LM2 | C2 |
SM3 | LM2 | C2 |
Manager Table:
manager_code | senior_manager_code | lead_manager_code | company_code |
---|---|---|---|
M1 | SM1 | LM1 | C1 |
M2 | SM3 | LM2 | C2 |
M3 | SM3 | LM2 | C2 |
Employee Table:
manager_code | manager_code | senior_manager_code | lead_manager_code | company_code |
---|---|---|---|---|
E1 | M1 | SM1 | LM1 | C1 |
E2 | M1 | SM1 | LM1 | C1 |
E3 | M2 | SM3 | LM2 | C2 |
E4 | M3 | SM3 | LM2 | C2 |
C1 Monika 1 2 1 2
C2 Samantha 1 1 2 2
In company C1, the only lead manager is LM1. There are two senior managers, SM1 and SM2, under LM1. There is one manager, M1, under senior manager SM1. There are two employees, E1 and E2, under manager M1.
In company C2, the only lead manager is LM2. There is one senior manager, SM3, under LM2. There are two managers, M2 and M3, under senior manager SM3. There is one employee, E3, under manager M2, and another employee, E4, under manager, M3.
SELECT
c.company_code,
c.founder,
COALESCE(lm.lead_manager_count, 0) AS lead_manager_count,
COALESCE(sm.senior_manager_count, 0) AS senior_manager_count,
COALESCE(m.manager_count, 0) AS manager_count,
COALESCE(e.employee_count, 0) AS employee_count
FROM
Company c
LEFT JOIN (
SELECT company_code, COUNT(DISTINCT lead_manager_code) AS lead_manager_count
FROM Lead_Manager
GROUP BY company_code
) lm ON c.company_code = lm.company_code
LEFT JOIN (
SELECT company_code, COUNT(DISTINCT senior_manager_code) AS senior_manager_count
FROM Senior_Manager
GROUP BY company_code
) sm ON c.company_code = sm.company_code
LEFT JOIN (
SELECT company_code, COUNT(DISTINCT manager_code) AS manager_count
FROM Manager
GROUP BY company_code
) m ON c.company_code = m.company_code
LEFT JOIN (
SELECT company_code, COUNT(DISTINCT employee_code) AS employee_count
FROM Employee
GROUP BY company_code
) e ON c.company_code = e.company_code
ORDER BY
c.company_code;
π 08/08/2024
π
You are given a table, BST, containing two columns: N and P, where N represents the value of a node in Binary Tree, and P is the parent of N.
Column | Type |
---|---|
N | Integer |
P | Integer |
Write a query to find the node type of Binary Tree ordered by the value of the node. Output one of the following for each node:
- Root: If node is root node.
- Leaf: If node is leaf node.
- Inner: If node is neither root nor leaf node.
N | P |
---|---|
1 | 2 |
3 | 2 |
6 | 8 |
9 | 8 |
2 | 5 |
8 | 5 |
5 | null |
1 Leaf
2 Inner
3 Leaf
5 Root
6 Leaf
8 Inner
9 Leaf
The Binary Tree below illustrates the sample:
5οΈβ£
_______|_______
/ \
2οΈβ£ 8οΈβ£
_____|_____ _____|_____
/ \ / \
1οΈβ£ 3οΈβ£ 6οΈβ£ 9οΈβ£
SELECT
N,
CASE
WHEN P IS NULL THEN 'Root'
WHEN N NOT IN (SELECT DISTINCT P FROM BST WHERE P IS NOT NULL) THEN 'Leaf'
ELSE 'Inner'
END AS NodeType
FROM
BST
ORDER BY
N;
π 08/08/2024
π
Pivot the Occupation column in OCCUPATIONS so that each Name is sorted alphabetically and displayed underneath its corresponding Occupation. The output column headers should be Doctor, Professor, Singer, and Actor, respectively.
Note: Print NULL when there are no more names corresponding to an occupation.
The OCCUPATIONS table is described as follows:
Column | Type |
---|---|
Name | String |
Occupation | String |
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
An OCCUPATIONS table that contains the following records:
Name | Occupation |
---|---|
Samantha | Doctor |
Julia | Actor |
Maria | Actor |
Meera | Singer |
Ashely | Professor |
Ketty | Professor |
Christeen | Professor |
Jane | Actor |
Jenny | Doctor |
Priya | Singer |
Jenny Ashley Meera Jane
Samantha Christeen Priya Julia
NULL Ketty NULL Maria
The first column is an alphabetically ordered list of Doctor names. The second column is an alphabetically ordered list of Professor names. The third column is an alphabetically ordered list of Singer names. The fourth column is an alphabetically ordered list of Actor names. The empty cell data for columns with less than the maximum number of names per occupation (in this case, the Professor and Actor columns) are filled with NULL values.
SELECT
MAX(CASE WHEN Occupation = 'Doctor' THEN Name ELSE NULL END) AS Doctor,
MAX(CASE WHEN Occupation = 'Professor' THEN Name ELSE NULL END) AS Professor,
MAX(CASE WHEN Occupation = 'Singer' THEN Name ELSE NULL END) AS Singer,
MAX(CASE WHEN Occupation = 'Actor' THEN Name ELSE NULL END) AS Actor
FROM (
SELECT
Name,
Occupation,
ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) AS RowNum
FROM OCCUPATIONS
) AS Subquery
GROUP BY RowNum
HAVING
MAX(CASE WHEN Occupation = 'Doctor' THEN Name ELSE NULL END) IS NOT NULL OR
MAX(CASE WHEN Occupation = 'Professor' THEN Name ELSE NULL END) IS NOT NULL OR
MAX(CASE WHEN Occupation = 'Singer' THEN Name ELSE NULL END) IS NOT NULL OR
MAX(CASE WHEN Occupation = 'Actor' THEN Name ELSE NULL END) IS NOT NULL
ORDER BY RowNum;
π 08/08/2024
π
Generate the following two result sets:
-
Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
-
Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:
There are a total of [occupation_count] [occupation]s.
where [occupation_count] is the number of occurrences of an occupation in OCCUPATIONS and [occupation] is the lowercase occupation name. If more than one Occupation has the same [occupation_count], they should be ordered alphabetically.
Note: There will be at least two entries in the table for each type of occupation.
The OCCUPATIONS table is described as follows:
Column | Type |
---|---|
Name | String |
Occupation | String |
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
An OCCUPATIONS table that contains the following records:
Name | Occupation |
---|---|
Samantha | Doctor |
Julia | Actor |
Maria | Actor |
Meera | Singer |
Ashely | Professor |
Ketty | Professor |
Christeen | Professor |
Jane | Actor |
Jenny | Doctor |
Priya | Singer |
Ashely(P)
Christeen(P)
Jane(A)
Jenny(D)
Julia(A)
Ketty(P)
Maria(A)
Meera(S)
Priya(S)
Samantha(D)
There are a total of 2 doctors.
There are a total of 2 singers.
There are a total of 3 actors.
There are a total of 3 professors.
The results of the first query are formatted to the problem description's specifications. The results of the second query are ascendingly ordered first by number of names corresponding to each profession (2<=2<=2<=3<=3), and then alphabetically by profession (doctor<=singer, and actor<=professor).
-- Requirement 1
SELECT
CONCAT(Name, '(', LEFT(Occupation, 1), ')') AS NameWithOccupation
FROM
OCCUPATIONS
ORDER BY
Name;
-- Requirement 2
SELECT
CONCAT('There are a total of ', COUNT(*)) AS occupation_count,
CONCAT(LOWER(Occupation), 's.') AS occupation
FROM
OCCUPATIONS
GROUP BY
Occupation
ORDER BY
occupation_count,
occupation;
π 07/29/2024
Given the CITY and COUNTRY tables, query the names of all cities where the CONTINENT is 'Africa'.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
SELECT CITY.NAME
FROM CITY
LEFT JOIN COUNTRY
ON CITY.COUNTRYCODE = COUNTRY.CODE
WHERE COUNTRY.CONTINENT = 'Africa';
π 07/29/2024
π
Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer.
SELECT DISTINCT(CITY)
FROM STATION
WHERE ID%2 = 0;
π 07/29/2024
π
A median is defined as a number separating the higher half of a data set from the lower half. Query the median of the Northern Latitudes (LAT_N) from STATION and round your answer to 4 decimal places.
SELECT ROUND(M.median, 4)
FROM (
SELECT LAT_N AS median
FROM (
SELECT LAT_N,
@row_num := @row_num + 1 AS row_num,
@total_rows := @row_num
FROM STATION, (SELECT @row_num := 0) r
ORDER BY LAT_N
) AS ordered
WHERE row_num IN (FLOOR((@total_rows + 1) / 2), CEIL((@total_rows + 1) / 2))
) AS M;
π 07/29/2024
Query the Western Longitude (LONG_W)where the smallest Northern Latitude (LAT_N) in STATION is greater than 38.7780. Round your answer to 4 decimal places.
SELECT ROUND(LONG_W, 4)
FROM STATION
WHERE LAT_N = (
SELECT MIN(LAT_N)
FROM STATION
WHERE LAT_N > 38.7780
);
π 07/29/2024
Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7780. Round your answer to 4 decimal places.
SELECT ROUND(MIN(LAT_N), 4)
FROM STATION
WHERE LAT_N > 38.7780;
π 07/29/2024
Query the Western Longitude (LONG_W) for the largest Northern Latitude (LAT_N) in STATION that is less than 137.2345. Round your answer to 4 decimal places.
SELECT ROUND(LONG_W, 4)
FROM STATION
WHERE LAT_N = (
SELECT MAX(LAT_N)
FROM STATION
WHERE LAT_N < 137.2345
);
π 07/29/2024
Given the CITY and COUNTRY tables, query the sum of the populations of all cities where the CONTINENT is 'Asia'.
Note: CITY.CountryCode and COUNTRY.Code are matching key columns.
SELECT SUM(CITY.POPULATION)
FROM CITY
LEFT JOIN COUNTRY
ON CITY.COUNTRYCODE = COUNTRY.CODE
WHERE COUNTRY.CONTINENT = 'Asia';
π 07/28/2024
Query the sum of Northern Latitudes (LAT_N) from STATION having values greater than 38.7880 and less than 137.2345. Truncate your answer to 4 decimal places.
SELECT TRUNCATE(SUM(LAT_N), 4)
FROM STATION
WHERE LAT_N > 38.7880
AND LAT_N < 137.2345;
π 07/28/2024
Query the greatest value of the Northern Latitudes (LAT_N) from STATION that is less than 137.2345. Truncate your answer to 4 decimal places.
SELECT
TRUNCATE(MAX(LAT_N), 4)
FROM
STATION
WHERE
LAT_N < 137.2345;
π 07/24/2024
Query the following two values from the STATION table:
- The sum of all values in LAT_N rounded to a scale of 2 decimal places.
- The sum of all values in LONG_W rounded to a scale of 2 decimal places.
LAT_N is the northern latitude and LONG_W is the western longitude.
Output Format
Your results must be in the form:
lat lon
where lat is the sum of all values in LAT_N and lon is the sum of all values in LONG_W. Both results must be rounded to a scale of 2 decimal places.
SELECT
ROUND(SUM(LAT_N), 2) AS lat,
ROUND(SUM(LONG_W), 2) AS lon
FROM STATION;
π 07/06/2024
We define an employee's total earnings to be their monthly salary x months
worked, and the maximum total earnings to be the maximum total earnings for any employee in the Employee
table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as 2 space-separated integers.
SELECT
MAX(earnings) AS max_earnings,
COUNT(*) AS num_employees
FROM (
SELECT
salary * months AS earnings
FROM
Employee
) AS TotalEarnings
WHERE
earnings = (
SELECT
MAX(salary * months)
FROM
Employee
);
π 07/06/2024
Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard's 0 key was broken until after completing the calculation. She wants your help finding the difference between her miscalculation (using salaries with any zeros removed), and the actual average salary.
Write a query calculating the amount of error (i.e.: average monthly salaries), and round it up to the next integer.
WITH ActualAverage AS (
SELECT AVG(salary) AS actual_avg_salary
FROM EMPLOYEES
),
MiscalculatedAverage AS (
SELECT AVG(CAST(REPLACE(CAST(salary AS CHAR), '0', '') AS DECIMAL)) AS miscalculated_avg_salary
FROM EMPLOYEES
)
SELECT CEIL(ABS(ActualAverage.actual_avg_salary - MiscalculatedAverage.miscalculated_avg_salary)) AS error
FROM ActualAverage, MiscalculatedAverage;
WITH, CAST, REPLACE, CEIL
π 07/04/2024
Query the difference between the maximum and minimum populations in CITY.
SELECT MAX(POPULATION) - MIN(POPULATION)
FROM CITY;
MAX, MIN
π 07/03/2024
Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.
SELECT (SUM(POPULATION))
FROM CITY
WHERE COUNTRYCODE = 'JPN';
SUM
π 07/01/2024
Query the average population for all cities in CITY, rounded down to the nearest integer.
SELECT ROUND(AVG(POPULATION), 0)
FROM CITY;
ROUND
π 06/30/2024
Query the average population of all cities in CITY where District is California.
SELECT AVG(POPULATION)
FROM CITY
WHERE DISTRICT = 'California;
π 06/29/2024
Query the total population of all cities in CITY where District is California.
SELECT SUM(POPULATION)
FROM CITY
WHERE District = 'California';
π 06/22/2024
SELECT
CASE
WHEN A + B <= C OR A + C <= B OR B + C <= A THEN 'Not A Triangle'
WHEN A = B AND B = C THEN 'Equilateral'
WHEN A = B OR B = C OR A = C THEN 'Isosceles'
ELSE 'Scalene'
END AS TriangleType
FROM TRIANGLES;
CASE, WHEN, END