Browse Source

done for today I think

master
Jared Dunbar 3 years ago
parent
commit
d9d29cae0c
Signed by: jared GPG Key ID: CF202CC859BAC692
  1. 82
      11-5-18.sql

82
11-5-18.sql

@ -1,27 +1,31 @@
-- in class examples
-- dataset is employee(name)
-- first, always see the data you are working with
SELECT * FROM employee;
-- then, see the size of the data
SELECT COUNT(*) FROM employee;
-- show winners of the nobel prize in 1950
-- dataset is nobel(yr, subject, winner) (where winner is name)
-- q1 show winners of the nobel prize in 1950
SELECT yr, subject, winner FROM nobel
WHERE yr = 1950;
-- show 1962 Literature prize winners
-- q2 show 1962 Literature prize winners
SELECT winner
FROM nobel
WHERE yr = 1950
WHERE yr = 1950winner, subject, year
AND subject = 'Literature';
-- find nobel prizes that Albert Einstein won
-- q3 find nobel prizes that Albert Einstein won
SELECT yr, subject
FROM nobel
WHERE winner = 'Albert Einstein';
-- Peace winners since 2000 inclusively
-- q4 Peace winners since 2000 inclusively
SELECT winner
FROM nobel
WHERE yr >= 2000
@ -92,33 +96,89 @@ SELECT yr, subject, winner
FROM nobel
WHERE subject <> 'Chemistry' -- not equal is <>
AND subject <> 'Medicine'
AND yr = 1980
AND yr = 1980;
-- another one I came up with
SELECT *
FROM nobel
WHERE yr = 1980 AND subject NOT IN ('Chemistry', 'Medicine')
WHERE yr = 1980 AND subject NOT IN ('Chemistry', 'Medicine');
-- another one he did
SELECT *
FROM nobel
WHERE subject NOT IN ('Chemistry', 'Medicine')
AND yr = 1980
AND yr = 1980;
-- q10 Show the info for the 'Medicine' prize before 1910, and Literature after 2004 inclusively
SELECT *
FROM nobel
WHERE (subject = 'Literature' AND yr >= 2004)
OR (subject = 'Medicine' AND yr < 1910)
OR (subject = 'Medicine' AND yr < 1910);
-- q11 Find all of the prizes by Peter Grünberg
SELECT *
FROM nobel
WHERE name = 'Peter Grünberg'
WHERE name = 'Peter Grünberg';
-- he didn't expect this to work, lol. I think this newer database engine uses UTF-8 encoding for the data, and thus there is no problem there
-- sad excuse for LIKE demonstrated here. He does mention it's bad to do this
SELECT *
FROM nobel
WHERE name LIKE 'Peter Gr%nberg'
WHERE name LIKE 'Peter Gr%nberg';
-- can also use CHAR(252) to show ü. Not sure why you would need to do this since you can type this particular character, but I guess if your name has newlines in it that this would matter.
SELECT *
FROM nobel
WHERE winner = 'Peter Gr' || CHAR(252) || 'nberg';
-- q12 find this other guy
SELECT *
FROM nobel
WHERE winner = 'Eugene O''Niell';
-- q13 find winners that have names that start with 'Sir', and in the order name, year, subject
SELECT winner, year, subject
FROM nobel
WHERE winner LIKE 'Sir %'
ORDER BY yr DESC, winner;
-- dataset is now world(name, continent, area, population, gdp)
-- q1 list each country name where the population is larger than that of Russia
SELECT name
FROM world
WHERE population > (
SELECT population
FROM world
WHERE name = 'Russia'
);
-- note - you can only expect one result to be returned from the nested query, otherwise it will fail. For example, returning all countries numbers that have an R to start the name of the country will fail.
-- could have also done the population straight.
SELECT name
FROM world
WHERE population > 1460000000;
-- "cheating"
-- q2 show the countries with a per capita GDP greater than the UK
SELECT name
FROM world
WHERE continent = 'Europe'
AND gdp/population > (
SELECT gdp/population AS percapitagdp
FROM world
WHERE name = 'United Kingdom'
);
-- q3 List the names and continent of countries in the continents containng either Argentena or Australia, and order by country
SELECT name, continent
FROM world
WHERE continent IN (
SELECT continent
FROM world
WHERE name IN ('Argentena', 'Australia')
)
ORDER BY name;
Loading…
Cancel
Save