Interview Questions -1

1 – Query the list of CITY names starting with vowels (i.e., aeio, or u) from STATION. Your result cannot contain duplicates.

1449345840-5f0a551030-Station.jpg

Answer : select city from station where city like ‘[aeiou]%’

 

2 – Query the names of all American cities in CITY with populations larger than 120000. The CountryCode for America is USA.

1449729804-f21d187d0f-CITY.jpg

Answer : select name from city where countrycode =’USA’ and population > 120000

 

3 – Query a list of CITY names from STATION with even ID numbers only. You may print the results in any order, but must exclude duplicates .  1449345840-5f0a551030-Station.jpg

Answer : select distinct city from station where id % 2=0 order by city

 

4 – 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.

Input Format

The STATION table is described as follows:

 

1449345840-5f0a551030-Station.jpg

Sample Output

ABC 3
PQRS 4

Answer :   select top 1 city,LEN(city) from STATION order by len(city),city;
select top 1 city,LEN(city) from STATION order by len(city) desc

 

5 – 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.

 

1443815209-cf4b260993-2.png

 

select name
from students
where marks>75 and name=’Stuart’
order by substring(name,len(name)-2, 3) , id asc;

 

Explanation  of Substring : SELECT substring(‘W3Schools.com’,LEN(‘W3Schools.com’)-2,3);    –> com  (13-2) =11 ,3 –> 11 to 3

substring(‘W3Schools.com’,LEN(‘W3Schools.com’)-1,3);    –> om  (12-2) =10  ,3  –> 10 to 3

substring(‘W3Schools.com’,LEN(‘W3Schools.com’)-3,3);    –> om  (12-3) =9  ,3  –>  9 to 12

 

 

Leave a comment