Browse Source

progress in class

master
Jared Dunbar 4 years ago
parent
commit
a89d195e83
Signed by: jared GPG Key ID: CF202CC859BAC692
  1. 93
      11-5-18.sql

93
11-5-18.sql

@ -1,53 +1,124 @@
-- in class examples
-- first, always see the data you are working with
SELECT * FROM employee
SELECT * FROM employee;
-- then, see the size of the data
SELECT COUNT(*) FROM employee
SELECT COUNT(*) FROM employee;
-- show winners of the nobel prize in 1950
SELECT yr, subject, winner FROM nobel
WHERE yr = 1950
WHERE yr = 1950;
-- show 1962 Literature prize winners
SELECT winner
FROM nobel
WHERE yr = 1950
AND subject = 'Literature'
AND subject = 'Literature';
-- find nobel prizes that Albert Einstein won
SELECT yr, subject
FROM nobel
WHERE winner = 'Albert Einstein'
WHERE winner = 'Albert Einstein';
-- Peace winners since 2000 inclusively
SELECT winner
FROM nobel
WHERE yr >= 2000
AND subject = 'Peace'
AND subject = 'Peace';
-- Good idea to show the criteria you are filtering on
SELECT winner, yr
FROM nobel
WHERE yr >= 2000
AND subject = 'Peace'
AND subject = 'Peace';
-- Show all details (year, subject winner) of winners from 1980 to 1989 of he Literature prize
-- q5 Show all details (year, subject winner) of winners from 1980 to 1989 of he Literature prize
SELECT *
FROM nobel
WHERE yr >= 1980
AND yr <= 1989
AND subject = 'Literature'
AND subject = 'Literature';
-- use something other than two ands - LIKE works
SELECT *
FROM nobel
WHERE yr LIKE '198%'
AND subject = 'Literature'
AND subject = 'Literature';
-- and we can also try this:
SELECT *
FROM nobel
WHERE yr BETWEEN 1980 AND 1989 -- this is inclusively specified
AND subject = 'Literature'
AND subject = 'Literature';
-- q6 show the details of these winners
SELECT *
FROM nobel
WHERE winner = 'Theodore Roosevelt'
OR winner = 'Woodrow Wilson'
OR winner = 'Jimmy Carter'
OR winner = 'Barack Obama';
-- better way, where winner IN (list of items)
SELECT *
FROM nobel
WHERE winner IN ('Theodore Roosevelt', 'Woodrow Wilson', 'Jimmy Carter', 'Barack Obama');
-- q7 Show the winners with the first name 'John'
SELECT winner
FROM nobel
WHERE winner LIKE 'John %';
-- q8 show the year, subject, and name of Physics winners for 1980 and the wnners of Chemistry for 1984
SELECT *
FROM nobel
WHERE (subject = 'Physics' AND yr = 1980)
OR (subject = 'Chemistry' AND yr = 1984)
-- another one I came up with
SELECT *
FROM nobel
WHERE yr = 1980
AND subject = 'Physics';
UNION
SELECT *
FROM nobel
WHERE yr = 1984
AND subject = 'Chemistry';
-- q9 Show all information about 1980 excluding Chemistry and Medicine
SELECT yr, subject, winner
FROM nobel
WHERE subject <> 'Chemistry' -- not equal is <>
AND subject <> 'Medicine'
AND yr = 1980
-- another one I came up with
SELECT *
FROM nobel
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
-- 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)
-- q11 Find all of the prizes by Peter Grünberg
SELECT *
FROM nobel
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'
Loading…
Cancel
Save