BASIC SELECT SQL QUERIES
Let me explain it with some "Hackerank" qns
QN: 1
Query all columns for a city in CITY with the ID 1661
ANS: SELECT * from CITY where ID = 1661;
since, it is to select all columns from table city.. and the condition is for ID = 1661.
QN: 2
Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.
ANS: SELECT * from CITY where COUNTRYCODE= 'JPN';
since, it is to select all attributes from the table city.. and the condition is for a COUNTRYCODE =JPN
QN:3
Query the NAME field for all American cities in the CITY table with populations larger than 120000. The CountryCode for America is USA.
ANS: SELECT NAME from CITY where POPULATION > 120000 and COUNTRYCODE = 'USA';
since it is to select all the names from the table city , condition is population is more than 120000 and CountryCode =USA
QN:4
Query the list of CITY names from STATION that do not start with vowels. Your result cannot contain duplicates.
ANS: SELECT DISTINCT CITY FROM STATION WHERE CITY NOT LIKE"A%" AND CITY NOT LIKE "E%"AND CITY NOT LIKE "I%"AND CITY NOT LIKE "O%"AND CITY NOT LIKE "U%";
Since, it is to select the unique city names from the table STATION and the condition is to the names of the city should not start with vowels(a,e,i,o,u).
so, A%,E%,I%,O%,U%.. is that all other character after that are considered . so, this represents the first leter.
QN:5
Query a list of CITY and STATE from the STATION table.
ANS: SELECT CITY,STATE FROM STATION;
100000. The CountryCode for America is USA.JPN
Comments
Post a Comment