My SQLQuerys
My SQLQuerys
Query all columns for all American cities in the CITY table with populations larger
than 100000.
The CountryCode for America is USA.
CITY
Field
ID
NAME
Type
NUMBER
VARCHAR2 ( 17)
COUNTRYCODE VARCHAR2(3)
DISTRICT
VARCHAR2 (20 )
POPULATION
NUMBER
SOLUTION:
===================================================================================
===================================
2.
Query the NAME field for all American cities in the CITY table with populations
larger than 120000.
The CountryCode for America is USA.
CITY
Field
ID
NAME
Type
NUMBER
VARCHAR2 ( 17)
COUNTRYCODE VARCHAR2 (3)
DISTRICT
VARCHAR2 ( 20)
POPULATION NUMBER
SOLUTION:
===================================================================================
===================================
3.
Query all columns (attributes) for every row in the CITY table.
CITY
Field
Type
ID
NUMBER
NAME
VARCHAR2 ( 17)
COUNTRYCODE VARCHAR2 (3)
DISTRICT
POPULATION
VARCHAR2 (20 )
NUMBER
SOLUTION:
===================================================================================
===================================
4.
CITY
Field
ID
Type
NUMBER
NAME
VARCHAR2 (17)
COUNTRYCODE VARCHAR2 (3)
DISTRICT
VARCHAR2 (20 )
POPULATION NUMBER
SOLUTION:
===================================================================================
===================================
5.
Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for
Japan is JPN.
CITY
Field
ID
Type
NUMBER
NAME
VARCHAR2 (17)
COUNTRYCODE VARCHAR2 (3)
DISTRICT
VARCHAR2 (20 )
POPULATION
NUMBER
SOLUTION:
===================================================================================
===================================
6.
Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for
Japan is JPN.
The CITY table is described as follows:
CITY
Field
ID
Type
NUMBER
NAME
VARCHAR2 ( 17)
COUNTRYCODE VARCHAR2(3)
DISTRICT
VARCHAR2 (20 )
POPULATION NUMBER
SOLUTION:
===================================================================================
===================================
7.
Field
ID
CITY
STATE
LAT_N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
SOLUTION:
===================================================================================
===================================
8.
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.
The STATION table is described as follows:
Field
ID
CITY
STATE
LAT_N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
SOLUTION:
===================================================================================
===================================
9.
Find the difference between the total number of CITY entries in the table and
the number of distinct CITY entries in the table.
The STATION table is described as follows:
Field
ID
CITY
STATE
LAT N
LONG W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
For example, if there are three records in the table with CITY values 'New York',
'New York', 'Bengalaru',
there are 2 different city names: 'New York' and 'Bengalaru'. The query returns 1,
because
total number of records - number of unique city names = 3 - 2 = 1.
SOLUTION:
===================================================================================
===================================
10.
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.
The STATION table is described as follows:
Field
ID
CITY
STATE
LAT N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
Sample Input
For example, CITY has four entries: DEF, ABC, PQRS and WXY.
Sample Output
ABC 3
PQRS 4
Explanation
When ordered alphabetically, the CITY names are listed as ABC, DEF, PQRS, and WXY,
with lengths 3,3,4 and 3. The longest name is PQRS, but there are options for
shortest named city. Choose ABC, because it comes first alphabetically.
Note
You can write two separate queries to get the desired output. It need not be a
single query.
SOLUTION:
===================================================================================
===================================
11.
Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from
STATION.
Your result cannot contain duplicates.
Input Format
Field
ID
CITY
STATE
LAT N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
SOLUTION:
===================================================================================
===================================
12.
Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your
result cannot contain duplicates.
Input Format
Field
ID
CITY
STATE
LAT_N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
SOLUTION:
===================================================================================
===================================
13.
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.
Input Format
Field
ID
CITY
STATE
LAT_N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
SOLUTION:
===================================================================================
===================================
14.
Query the list of CITY names from STATION that do not start with vowels. Your
result cannot contain duplicates.
Input Format
Field
ID
CITY
STATE
LAT N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
SOLUTION:
===================================================================================
===================================
15.
Query the list of CITY names from STATION that do not end with vowels. Your result
cannot contain duplicates.
Input Format
Field
ID
CITY
STATE
LAT_N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
SOLUTION:
select distinct city
from station
where city NOT RLIKE '[aeiouAEIOU]$'
===================================================================================
===================================
16.
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.
Input Format
Field
ID
CITY
STATE
LAT_N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
SOLUTION:
===================================================================================
===================================
17.
Query the list of CITY names from STATION that do not start with vowels and do not
end with vowels. Your result cannot contain duplicates.
Input Format
Field
ID
CITY
STATE
LAT_N
LONG_W
STATION
Type
NUMBER
VARCHAR2 (21)
VARCHAR2 (2)
NUMBER
NUMBER
where LAT_N is the northern latitude and LONG_W is the western longitude.
SOLUTION:
===================================================================================
===================================
18.
Query the Name of any student in STUDENTS who scored higher than 75 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.
Input Format
Column
ID
Name
Marks
Type
Integer
String
Integer
The Name column only contains uppercase (A-Z) and lowercase (a-z) letters.
Sample Input
ID
1
2
4
3
Name
Ashley
Samantha
Julia
Belvet
Marks
81
75
76
84
Sample Output
Ashley
Julia
Belvet
Explanation
SOLUTION:
SELECT name
from students
where marks > 75
order by RIGHT(name, 3), id asc
===================================================================================
===================================
19.
Write a query that prints a list of employee names (i.e.: the name attribute) from
the Employee table in alphabetical order.
Input Format
The Employee table containing employee data for a company is described as follows:
Column
employee_id
name
months
salary
Type
Integer
String
Integer
Integer
Sample Input
employee_id
12228
33645
45692
56118
59725
74197
78454
83565
98607
99989
name
Rose
Angela
Frank
Patrick
Lisa
Kimberly
Bonnie
Michael
Todd
Joe
months
15
1
17
7
11
16
8
6
5
9
salary
1968
3443
1608
1345
2330
4372
1771
2017
3396
3573
Sample Output
Angela
Bonnie
Frank
Joe
Kimberly
Lisa
Michael
Patrick
Rose
Todd
SOLUTION:
select name
from employee
order by name asc
===================================================================================
===================================
20.
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.
Input Format
The Employee table containing employee data for a company is described as follows:
Column
employee_id
name
months
salary
Type
Integer
String
Integer
Integer
Sample Input
employee_id
12228
33645
45692
56118
59725
74197
78454
83565
98607
99989
name
Rose
Angela
Frank
Patrick
Lisa
Kimberly
Bonnie
Michael
Todd
Joe
months
15
1
17
7
11
16
8
6
5
9
salary
1968
3443
1608
1345
2330
4372
1771
2017
3396
3573
Sample Output
Angela
Michael
Todd
Joe
Explanation
Angela has been an employee for 1 month and earns $3443 per month.
Michael has been an employee for 6 months and earns $2017 per month.
Todd has been an employee for 5 months and earns $3396 per month.
Joe has been an employee for 9 months and earns $3573 per month.
SOLUTION:
select name
from employee
where salary > 2000 and months < 10
order by employee_id asc
===================================================================================
===================================
21.
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.
Input Format
CITY
Field
Type
ID
NUMBER
NAME
VARCHAR2 ( 17)
COUNTRYCODE VARCHAR2(3)
DISTRICT
VARCHAR2 (20 )
POPULATION
NUMBER
Field
CODE
NAME
CONTINENT
REGION
SURFACEAREA
INDEPYEAR
POPULATION
LIFEEXPECTANCY
GNP
GNPOLD
LOCALNAME
GOVERNMENTFORM
HEADOFSTATE
CAPITAL
CODE2
COUNTRY
Type
VARCHAR2 (3)
VARCHAR2 (44)
VARCHAR2 (13)
VARCHAR2 (25)
NUMBER
VARCHAR2 (5)
NUMBER
VARCHAR2 (4)
NUMBER
VARCHAR2 (9)
VARCHAR2 (44)
VARCHAR2 (44)
VARCHAR2 (32)
VARCHAR2 (4)
VARCHAR2 (2)
SOLUTION:
===================================================================================
===================================
22.
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:
Column
C
Type
Integer
Integer
Integer
Each row in the table denotes the lengths of each of a triangle's three sides.
Sample Input
A
20
20
20
13
B
20
20
21
14
23
20
22
30
Sample Output
Isosceles
Equilateral
Scalene
Not A Triangle
Explanation
SOLUTION:
SELECT
CASE
WHEN A=B AND B=C THEN "Equilateral"
WHEN (A+B<=C) OR (B+C<=A) OR (A+C<=B) THEN "Not A Triangle"
WHEN A=B OR A=C OR C=B THEN "Isosceles"
ELSE "Scalene"
END
FROM Triangles;
===================================================================================
===================================
23.
Generate the following two result sets:
Note: There will be at least two entries in the table for each type of occupation.
Input Format
Note: There will be at least two entries in the table for each type of occupation.
Input Format
Column
Name
Occupation
Type
String
String
Occupation will only contain one of the following values: Doctor, Professor, Singer
or Actor.
Sample Input
Name
Samantha
Julia
Maria
Meera
Ashely
Ketty
Christeen
Jane
Jenny
Priya
Occupation
Doctor
Actor
Actor
Singer
Professor
Professor
Professor
Actor
Doctor
Singer
Sample Output
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.
Explanation
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≤3≤3),
and then alphabetically by profession (doctor ≤ singer, and actor ≤ professor).
SOLUTION:
SELECT CONCAT('There are a total of', ' ', COUNT(Occupation), ' ',
LOWER(Occupation),'s.')
FROM OCCUPATIONS
GROUP BY Occupation
ORDER BY COUNT(Occupation);
===================================================================================
===================================
24.
Note: Print NULL when there are no more names corresponding to an occupation.
Input Format
The OCCUPATIONS table is described as follows:
Column
Name
Occupation
Type
String
String
Occupation will only contain one of the following values: Doctor, Professor, Singer
or Actor.
Sample Input
Name
Samantha
Julia
Maria
Meera
Ashely
Ketty
Christeen
Jane
Jenny
Priya
Occupation
Doctor
Actor
Actor
Singer
Professor
Professor
Professor
Actor
Doctor
Singer
Sample Output
SOLUTION:
-- Common Table Expression (CTE) to assign row numbers based on occupation and name
with cte as (
select
name,
occupation,
row_number() over(partition by occupation order by name) as rnk
from occupations
order by 2, 3 -- Order the result set by occupation and row number
)
-- Selecting the maximum name for each occupation using conditional aggregation
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 rnk -- Grouping by row number to get one row per occupation
===================================================================================
===================================
25.
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.
Input Format
CITY
Field
Type
ID
NUMBER
NAME
VARCHAR2 (17)
COUNTRYCODE VARCHAR2(3)
DISTRICT
VARCHAR2 (20 )
POPULATION
NUMBER
Field
CODE
NAME
CONTINENT
REGION
SURFACEAREA
INDEPYEAR
POPULATION
LIFEEXPECTANCY
GNP
GNPOLD
LOCALNAME
GOVERNMENTFORM
HEADOFSTATE
CAPITAL
CODE2
COUNTRY
Type
VARCHAR2 (3)
VARCHAR2 (44)
VARCHAR2 (13)
VARCHAR2 (25)
NUMBER
VARCHAR2 (5)
NUMBER
VARCHAR2 (4)
NUMBER
VARCHAR2 (9)
VARCHAR2 (44)
VARCHAR2 (44)
VARCHAR2 (32)
VARCHAR2 (4)
VARCHAR2 (2)
SOLUTION:
===================================================================================
===================================
26.
You are given two tables: Students and Grades. Students contains three columns ID,
Name and Marks.
Column
ID
Name
Marks
Type
Integer
String
Integer
Grade
1
2
3
4
5
6
7
8
9
10
Min_Mark
Max_Mark
9
10
20
30
40
50
60
70
80
90
29
39
49
59
69
79
89
100
Ketty gives Eve a task to generate a report containing three columns: Name, Grade
and Mark.
Ketty doesn't want the NAMES of those students who received a grade lower than 8.
The report must be in descending order by grade -- i.e. higher grades are entered
first.
If there is more than one student with the same grade (8-10) assigned to them,
order those particular students by their name alphabetically.
Finally, if the grade is lower than 8, use "NULL" as their name and list them by
their grades in descending order.
If there is more than one student with the same grade (1-7) assigned to them, order
those particular students by their marks in ascending order.
Sample Input
ID
1
2
3
4
5
6
Name
Julia
Samantha
Maria
Scarlet
Ashley
Jane
Marks
88
68
99
78
63
81
Sample Output
Maria 10 99
Jane 9 81
Julia 9 88
Scarlet 8 78
NULL 7 63
NULL 7 68
Note
Explanation
Consider the following table with the grades assigned to the students:
ID
1
2
3
4
5
6
Name
Julia
Samantha
Maria
Scarlet
Ashley
Jane
Marks
88
68
99
78
63
81
Grade
9
7
10
8
7
9
SOLUTION:
===================================================================================
===================================
27.