The CITY table is described as follows:
My Solution (MySQL):
SELECT * FROM CITY;
My Solution (MySQL):
SELECT * FROM CITY WHERE ID =1661;
My Solution (MySQL):
SELECT * FROM CITY WHERE COUNTRYCODE = 'JPN';
My Solution (MySQL):
SELECT NAME FROM CITY WHERE COUNTRYCODE = 'JPN';
My Solution (MySQL):
SELECT NAME FROM CITY WHERE COUNTRYCODE = 'USA';
Q6.Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA.
My Solution (MySQL):
SELECT * FROM CITY WHERE POPULATION > 100000 AND COUNTRYCODE = 'USA';
Q7.Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.
My Solution (MySQL):
SELECT NAME FROM CITY WHERE POPULATION > 120000 AND COUNTRYCODE = 'USA';
The STATION table is described as follows:
My Solution (MySQL):
SELECT CITY, STATE FROM STATION;
Q9.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.
My Solution (MySQL):
SELECT DISTINCT CITY FROM STATION WHERE MOD(ID, 2) = 0;
Q10.Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.
My Solution (MySQL):
SELECT COUNT(CITY) - COUNT(DISTINCT(CITY)) FROM STATION;
Q11.Query the two cities in STATION with the shortest and longest CITY names, as well as their respective lengths (i.e.: number of characters in the name). If there is more than one smallest or largest city, choose the one that comes first when ordered alphabetically.
My Solution (MySQL):
SELECT CITY, LENGTH(CITY) FROM STATION WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) FROM STATION)
OR LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) FROM STATION)
ORDER BY LENGTH(CITY) DESC, CITY ASC LIMIT 2;
Q12.Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates.
My Solution (MySQL):
SELECT DISTINCT CITY FROM STATION
WHERE CITY REGEXP '^[A,E,I,O,U]';
Q13.Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.
My Solution (MySQL):
SELECT DISTINCT CITY FROM STATION
WHERE CITY REGEXP '[A,E,I,O,U]$';
Q14.Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.
My Solution (MySQL):
SELECT DISTINCT CITY FROM STATION
WHERE CITY REGEXP '^[A,E,I,O,U].*[A,E,I,O,U]$';
Q15.Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
My Solution (MySQL):
SELECT DISTINCT CITY FROM STATION
WHERE CITY NOT REGEXP '^[A,E,I,O,U]';
Q16.Query the list of CITY names from STATION that either do not start with vowels or do not end with vowels. Your result cannot contain duplicates.
My Solution (MySQL):
SELECT DISTINCT CITY FROM STATION
WHERE CITY NOT REGEXP '^[A,E,I,O,U].*[A,E,I,O,U]$';
Q17.Query the list of CITY names from STATION that either do not start with vowels and do not end with vowels. Your result cannot contain duplicates.
My Solution (MySQL):
SELECT DISTINCT CITY FROM STATION
WHERE CITY REGEXP '^[^A,E,I,O,U]' AND REGEXP '[^A,E,I,O,U]$';
Q18.Query the Name of any student in STUDENTS who scored higher than Marks. Order your output by the last three characters of each name. If two or more students both have names ending in the same last three characters (i.e.: Bobby, Robby, etc.), secondary sort them by ascending ID.
The STUDENTS table is described as follows:
My Solution (MySQL):
SELECT NAME FROM STUDENTS
WHERE MARKS > 75
ORDER BY RIGHT(NAME,3), ID;
Q19.Write a query that prints a list of employee names (i.e.: the name attribute) from the Employee table in alphabetical order.
The Employee table containing employee data for a company is described as follows:
My Solution (MySQL):
SELECT name FROM Employee
ORDER BY name ASC;
Q20.Write a query that prints a list of employee names (i.e.: the name attribute) for employees in Employee having a salary greater than $2000 per month who have been employees for less than 10 months. Sort your result by ascending employee_id.
My Solution (MySQL):
SELECT name FROM Employee
WHERE salary > 2000 AND months < 10
ORDER BY employee_id ASC;
COUNT() FUNCTION-
The CITY table is described as follows:
My Solution (MySQL):
SELECT COUNT(*) FROM CITY
WHERE POPULATION > 1000000;
SUM() FUNCTION-
My Solution (MySQL):
SELECT SUM(POPULATION) FROM CITY
WHERE DISTRICT = 'California';
AVG() FUNCTION-
My Solution (MySQL):
SELECT AVG(POPULATION) FROM CITY
WHERE DISTRICT = 'California';
AVG() FUNCTION-
My Solution (MySQL):
SELECT ROUND(AVG(POPULATION)) FROM CITY;
Q25. Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.
My Solution (MySQL):
SELECT SUM(POPULATION) FROM CITY
WHERE COUNTRYCODE = 'JPN';
My Solution (MySQL):
SELECT MAX(POPULATION) - MIN(POPULATION) FROM CITY;
Q27.Samantha was tasked with calculating the average monthly salaries for all employees in the EMPLOYEES table, but did not realize her keyboard's 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.: actual - miscalculated average monthly salaries), and round it up to the next integer.
The EMPLOYEES table is described as follows:
My Solution (MySQL):
SELECT CEIL(AVG(Salary)) - AVG(REPLACE(Salary,0,''))
FROM Employees;
Q28.We define an employee's total earnings to be their monthly 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 space-separated integers.
The Employee table containing employee data for a company is described as follows:
My Solution (MySQL):
SELECT MAX(salary*months) AS max_salary, COUNT(*) AS count_max_earning
FROM EMPLOYEE
WHERE salary*months = (SELECT max(salary*months) FROM EMPLOYEE);
1.The sum of all values in LAT_N rounded to a scale of 2 decimal places.
2.The sum of all values in LONG_W rounded to a scale of 2 decimal places.
The STATION table is described as follows:
My Solution (MySQL):
SELECT ROUND(SUM(LAT_N),2), ROUND(SUM(LONG_W),2) FROM STATION;
Q30.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.
My Solution (MySQL):
SELECT TRUNCATE(SUM(LAT_N),4) FROM STATION
WHERE LAT_N > 38.7880 AND LAT_N < 13.2345;
Q31.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.
My Solution (MySQL):
SELECT TRUNCATE(MAX(LAT_N),4) FROM STATION
WHERE LAT_N < 137.2345;
Q32.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.
My Solution (MySQL):
SELECT ROUND(LAT_N,4) FROM STATION
WHERE LAT_N = (SELECT MAX(LAT_N) FROM STATION WHERE LAT_N < 137.2345;
Q33.Query the smallest Northern Latitude (LAT_N) from STATION that is greater than 38.7780. Round your answer to 4 decimal places.
My Solution (MySQL):
SELECT ROUND(LAT_N,4) FROM STATION
WHERE LAT_N = (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N < 137.2345);
Q34.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
My Solution (MySQL):
SELECT ROUND(LONG_W,4) FROM STATION
WHERE LAT_N = (SELECT MIN(LAT_N) FROM STATION WHERE LAT_N > 38.7780);
-
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 P1 and P2 and round it to a scale of 4 decimal places.
My Solution (MySQL):
SELECT ROUND((MAX(LAT_N)-MIN(LAT_N)) + (MAX(LONG_W)-MIN(LONG_W)),4) AS Distance FROM STATION;
Q36.Consider P1(a,c) and P2(b,d) to be two points on a 2D plane where (a,b) are the respective minimum and maximum values of Northern Latitude (LAT_N) and (c,d) are the respective minimum and maximum values of Western Longitude (LONG_W) in STATION.
Query the Euclidean Distance between points P1 and P2 and format your answer to display 4 decimal digits.
My Solution (MySQL):
SELECT ROUND(SQRT(POWER(MAX(LAT_N)-MIN(LAT_N),2) + POWER(MAX(LONG_W)-MIN(LONG_W),2)),4) FROM STATION;
Q37.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.
My Solution (MySQL):
SELECT CAST(ROUND(AVG(LAT_N),4) AS DECIMAL(10,4)) FROM (SELECT LAT_N, ROW_NUMBER() OVER(ORDER BY LAT_N) AS RowNumber,
COUNT(*) OVER() AS TotalRows FROM STATION) AS temp
WHERE RowNumber IN ((TotalRows + 1) / 2, (TotalRows + 2) / 2);
Q38.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.
The CITY and COUNTRY tables are described as follows:
My Solution (MySQL):
SELECT SUM(CITY.POPULATION) FROM CITY
JOIN COUNTRY
ON CITY.COUNTRYCODE = COUNTRY.CODE
WHERE CONTINENT = 'Asia';
Q39.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.
My Solution (MySQL):
SELECT CITY.NAME FROM CITY
JOIN COUNTRY
ON CITY.COUNTRYCODE = COUNTRY.CODE
WHERE CONTINENT = 'Africa';
Q40.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.
My Solution (MySQL):
SELECT COUNTRY.CONTINENT, FLOOR(AVG(CITY.POPULATION FROM COUNTRY
JOIN CITY
ON COUNTRY.CODE = CITY.COUJNTRYCODE
GROUP BY COUNTRY.CONTIENT;
My Solution (MySQL):
SELECT COUNT(*) AS NUMBER_OF_CITY FROM CITY
WHERE POPULATION > 100000;
My Solution (MySQL):
SELECT SUM(POPULATION) AS TOTAL_POPULATION FROM CITY
WHERE DISTRICT = 'California';
My Solution (MySQL):
SELECT AVG(POPULATION) AS AVG_POPULATION FROM CITY
WHERE DISTRICT = 'California';
My Solution (MySQL):
SELECT ROUND(AVG(POPULATION)) FROM CITY;
Q45.Query the sum of the populations for all Japanese cities in CITY. The COUNTRYCODE for Japan is JPN.
My Solution (MySQL):
SELECT SUM(POPULATION) FROM CITY
WHERE COUNTRYCODE = 'JPN';
My Solution (MySQL):
SELECT MAX(POPULATION)-MIN(POPULATION) FROM CITY;
Q.Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:
- Equilateral: It's a triangle with 3 sides of equal length.
- Isosceles: It's a triangle with 2 sides of equal length.
- Scalene: It's a triangle with 3 sides of differing lengths.
- Not A Triangle: The given values of A, B, and C don't form a triangle.
The TRIANGLES table is described as follows:
Each row in the table denotes the lengths of each of a triangle's three sides.
Sample Input
My Solution (MySQL):
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 C = A THEN 'Isosceles'
END AS triangle_type
FROM TRIANGLES;
Q.Amber's conglomerate corporation just acquired some new companies. Each of the companies follows this hierarchy:
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.
- Lead_Manager: The lead_manager_code is the code of the lead manager, and the company_code is the code of the working company.
- 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.
- 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.
- 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.
My Solution (MySQL):
SELECT C.company_code, C.founder,
COUNT(DISTINCT E.lead_manager_code),
COUNT(DISTINCT E.senior_manager_code),
COUNT(DISTINCT E.manager_code),
COUNT(DISTINCT E.employee_code)
FROM Company C
INNER JOIN Employee E
ON C.company_code = E.company_code
GROUP BY C.company_code, C.founder
ORDER BY C.company_code;
Q.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.
The OCCUPATIONS table is described as follows:
Occupation will only contain one of the following values: Doctor, Professor, Singer or Actor.
My Solution (MySQL):
WITH CTE AS(SELECT name,occupation,ROW_NUMBER() OVER (PARTITION BY Occupation ORDER BY Name) as R1 FROM OCCUPATIONS)
SELECT MAX(CASE WHEN Occupation = "Doctor" THEN Name END) AS Doctor,
MAX(CASE WHEN Occupation = "Professor" THEN Name END) AS Professor,
MAX(CASE WHEN Occupation = "Singer" THEN Name END) AS Singer,
MAX(CASE WHEN Occupation = "Actor" THEN Name END) AS Actor
FROM CTE
GROUP BY R1;
1.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).
2.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.
My Solution (MySQL):
(select concat(name,'(',left(occupation,1),')')
from occupations
order by name limit 100)
union
( SELECT CONCAT("There are a total of ",count(*)," ",lower(occupation),"s.")
FROM OCCUPATIONS group by(occupation) order by count(occupation),occupation limit 100);