SQL Saturday

This is a short post but the problems were still pretty fun!

Types of Triangles:

Given the table of triangles, sides find if a triangle is possible. If so then find the type of triangle that exists.

Triangle


The PADS

An OCCUPATIONS table that contains the following records:

Generate the following two result sets:

  1. Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession as a parenthetical (i.e.: enclosed in parentheses). For example: AnActorName(A), ADoctorName(D), AProfessorName(P), and ASingerName(S).
  2. Query the number of ocurrences of each occupation in OCCUPATIONS. Sort the occurrences in ascending order, and output them in the following format:

SQL Saturday

SQL Saturday

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

SELECT DISTINCT CITY

FROM STATION

WHERE City like “A%” or City like “E%” or City like “I%” or City like “O%” or City like”U%”;

Query the list of CITY names ending with vowels (a, e, i, o, u) from STATION. Your result cannot contain duplicates.

SELECT DISTINCT CITY
FROM STATION
WHERE City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u";

Query the list of CITY names from STATION which has vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates.

SELECT DISTINCT CITY
FROM STATION
WHERE City like "A%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or
      City like "E%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or 
      City like "I%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or 
      City like "O%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u") or 
      City like"U%" AND (City like "%a" or City like "%e" or City like "%i" or City like "%o" or City like"%u");

Query the list of CITY names from STATION that does not start with vowels. Your result cannot contain duplicates.

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%";

Query the list of CITY names from STATION that does not end with vowels. Your result cannot contain duplicates.

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";

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;