Skip to content

Instantly share code, notes, and snippets.

@natevenn
Last active March 25, 2016 14:04
Show Gist options
  • Save natevenn/23912032ac47881ba130 to your computer and use it in GitHub Desktop.
Save natevenn/23912032ac47881ba130 to your computer and use it in GitHub Desktop.
Intermediate sql

###What's the total revenue for all items?

  • SELECT sum(revenue) FROM items;

###What's the average revenue for all items?

  • SELECT avg(revenue) FROM items;

###What's the minimum revenue for all items?

  • SELECT min(revenue) FROM items;

###What's the maximum revenue for all items?

  • SELECT max(revenue) FROM items;

###What the count for items with a name?

  • SELECT max(revenue) FROM items;

##Building on Aggregate Functions

###Return all main courses.

  • SELECT * FROM items WHERE course = 'main';
  • the syntax is very frustrating. I tried using double quotes on main it didnt work!

###Return only the names of the main courses

  • SELECT name FROM items WHERE course = 'main';

###Return the min and max value for the main courses.

  • SELECT max(revenue), min(revenue) from items WHERE course = 'main';

###What's the total revenue for all main courses?

  • SELECT sum(revenue) from items WHERE course = 'main';

##Inner joins

###Can you get it to display only the name for the item and the name for the season?

  • SELECT items.name, seasons.name FROM items INNER JOIN seasons ON items.season_id = seasons.id;

###Can you customize each heading using AS?

  • SELECT items.name as item_name, seasons.name AS season_name FROM items INNER JOIN seasons ON items.season_id = seasons.id;

##What is inner join, left outer join, and right outer join?

###What is a inner join?

  • Similar to a joins it connects two tables together that have matching columns. Example being primary key and its foreign key referenced in a seperate table.

###what is a left outer join?

  • It returns everything from the left side of the joins even if there is no match to the right side of the joins. If there is no match in a column on the right side it will display null

###what is a right outer join?

  • This is the opposite from left outer join. It returns everything from the right side of the joins even if there is no match to the left side of the joins. O matches will return null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment