Skip to content

Instantly share code, notes, and snippets.

@julyytran
Last active March 24, 2016 22:50
Show Gist options
  • Save julyytran/93fb5511940235a2721b to your computer and use it in GitHub Desktop.
Save julyytran/93fb5511940235a2721b 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 count(name) FROM items;

Return all main courses. Hint: What ActiveRecord method would you use to get this?

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

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

What is an INNER JOIN?

  • Creates a table of the records that are matching from both tables being joined

What is a LEFT OUTER JOIN?

  • Creates a table that contains all records in the left table (the one afer FROM), even if there is no match in the right table

What is a RIGHT OUTER JOIN?

  • Creates a table that contains all records in the right table (the one not afer FROM), even if there is no match in the left table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment