Fun with Select Queries

Query all columns for all American cities in CITY with populations larger than 100000. The CountryCode for America is USA. Input Format The CITY table is described as follows:

Screen Shot 2018-01-27 at 9.53.29 AM.png

SELECT * /*Selects all columns from table*/
FROM CITY /*selects table named city */
 /*only if the population is over100000 and the country is in the USA*/
WHERE POPULATION > 100000 AND COUNTRYCODE = 'USA';

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

SELECT NAME /*Selects NAME column from table*/
FROM CITY /*selects table named city */
/*only if the population is over 120000 and the country is in the USA*/
WHERE POPULATION > 120000 AND COUNTRYCODE = 'USA';

Query all columns (attributes) for every row in the CITY table.

SELECT * /*Selects all columns from table*/
FROM CITY; /*selects table named city */

Query all columns for a city in CITY with the ID 1661.

SELECT * /*Selects all columns from table*/
From CITY /*selects table named city */
WHERE ID = 1661; /*only if the city has an id of 1661 */

Query all attributes of every Japanese city in the CITY table. The COUNTRYCODE for Japan is JPN.

SELECT * /*Selects all columns from table*/
FROM CITY/*selects table named city */
WHERE COUNTRYCODE = 'JPN'; /*only if the country is in the japan*/

Query the names of all the Japanese cities in the CITY table. The COUNTRYCODE for Japan is JPN.

SELECT NAME /*Selects NAME column from table*/
FROM CITY/*selects table named city */
WHERE COUNTRYCODE = 'JPN'; /*only if the country is Japan*/

Query a list of CITY and STATE from the STATION table.

Screen Shot 2018-01-27 at 10.12.18 AM.png
SELECT CITY, STATE /*Selects the city and state columns from table*/
FROM STATION; /*reads from table*/

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 from your answer.
SELECT DISTINCT CITY /*selects distinct city name from a table */
FROM STATION /*uses table called station*/
WHERE (ID%2) = 0; /*only if the id is even*/

Let  be the number of CITY entries in STATION, and let  be the number of distinct CITY names in STATION; query the value of  from STATION. In other words, find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table.

/*selects city count and distinct city name from a table and finds diff*/
SELECT COUNT(CITY) - COUNT( DISTINCT CITY) FROM STATION;

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.

/*selects city name and string len from a table */
SELECT CITY, LENGTH(CITY)
/*selects from table named station */
FROM STATION
/*if the string len is the minimum */
WHERE LENGTH(CITY) = (SELECT MIN(LENGTH(CITY)) FROM STATION )
/*orders in alpha by city name */
ORDER BY CITY
/*takes only one from the list*/
LIMIT 1;

/*selects city name and string len from a table */
SELECT CITY, LENGTH(CITY)
/*selects from table named station */
FROM STATION 
/*if the string len is the maximum */
WHERE LENGTH(CITY) = (SELECT MAX(LENGTH(CITY)) FROM STATION)
/*orders in alpha by city name */
ORDER BY CITY
/*takes only one from the list*/
LIMIT 1;