SQL HR
SQL HR
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:
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', 'Bengaluru', there are 2 different city names: 'New York' and
'Bengaluru'. The query returns 1.
Ans:
Select Count(CITY) - Count(Distinct CITY) AS City_Count_Difference From STATION;
2. **COUNT(DISTINCT CITY)**: This function counts the number of distinct entries in the CITY column. It
counts each unique city name only once, regardless of how many times it appears in the table.
3. **COUNT(CITY) - COUNT(DISTINCT CITY)**: This performs subtraction between the total count of city
entries and the count of distinct city entries. It gives the count of duplicate city entries in the table.
In summary, the query calculates the difference between the total count of city entries and the count of
distinct city entries, effectively giving the count of duplicate city entries in the STATION table.
Que:
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.
Ans:
Select Distinct CITY From STATION WHERE MOD(ID,2) = 0;
The logic:
SELECT DISTINCT CITY: This part of the query selects distinct city names from the STATION table. The
DISTINCT keyword ensures that each city appears only once in the result set.
FROM STATION: This specifies the table from which we're selecting data, which is the STATION table.
WHERE MOD(ID, 2) = 0: This is the conditional filtering criteria. The MOD() function returns the
remainder of dividing the ID column by 2. So, MOD(ID, 2) calculates the modulo of the ID with 2. When
MOD(ID, 2) = 0, it means the ID is divisible evenly by 2 (i.e., there is no remainder when divided by 2).
This condition filters out only those rows where the ID is even.
In simpler terms, this query retrieves the names of cities from the STATION table where the ID of the
station is an even number.
Que:
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 :
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 and .
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.
Ans:
SELECT CITY, LENGTH(CITY) AS NAME_LENGTH
FROM STATION
ORDER BY NAME_LENGTH, CITY
LIMIT 1;
Both queries follow a similar structure but differ in the ordering criteria for selecting either the shortest
or longest city name. They ensure that only one city name is returned for each query, even if there are
ties.
Que: