0% found this document useful (0 votes)
7 views

SQL HR

Uploaded by

rawn8600
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQL HR

Uploaded by

rawn8600
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Que:

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;

Here's the logic behind the query:


1. **COUNT(CITY)**: This function counts the total number of entries in the CITY column. It counts every
occurrence of a city name in the table.

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.

4. **AS City_Count_Difference**: This aliases the result of the subtraction operation as


`City_Count_Difference`, providing a clear label for the calculated value in the result set.

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;

SELECT CITY, LENGTH(CITY) AS NAME_LENGTH


FROM STATION
ORDER BY NAME_LENGTH DESC, CITY
LIMIT 1;
Here's the logic behind the query:
1. **Shortest City Name**:
- `SELECT CITY, LENGTH(CITY) AS NAME_LENGTH`: This selects the city names from the `STATION` table
along with their corresponding lengths, where the `LENGTH()` function calculates the number of
characters in each city name.
- `FROM STATION`: Specifies the table from which the data is retrieved, in this case, the `STATION`
table.
- `ORDER BY NAME_LENGTH, CITY`: Orders the results first by the length of the city name in ascending
order (`NAME_LENGTH`), and then by the city name itself in alphabetical order (`CITY`). This ensures that
if there are multiple cities with the same shortest length, they will be ordered alphabetically.
- `LIMIT 1`: Limits the result set to only one row, which will be the city with the shortest name
according to the specified ordering.

2. **Longest City Name**:


- `SELECT CITY, LENGTH(CITY) AS NAME_LENGTH`: Similar to the first query, selects the city names
along with their lengths.
- `FROM STATION`: Specifies the table from which the data is retrieved, which is again the `STATION`
table.
- `ORDER BY NAME_LENGTH DESC, CITY`: Orders the results first by the length of the city name in
descending order (`NAME_LENGTH DESC`), meaning the longest names will come first, and then by the
city name itself in alphabetical order (`CITY`). This ensures that if there are multiple cities with the same
longest length, they will be ordered alphabetically.
- `LIMIT 1`: Limits the result set to only one row, which will be the city with the longest name according
to the specified ordering.

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:

You might also like