External Exam Download Resources Web Applications Games Recycle Bin

Subqueries

subqueries1.sql

SELECT title, calories
FROM menu
WHERE calories > (
	SELECT ROUND(AVG(calories))
	FROM menu
)

subqueries2.sql

SELECT title
FROM menu
WHERE price / calories >= ( /* all ratios better than: */
	SELECT price / calories AS "cost effective ratio" /* note - this alias redundant */
	FROM menu
	ORDER BY 1 DESC
	LIMIT 1 OFFSET 2  /* to get 3rd best ratio */
)

subqueries3.sql

SELECT COUNT(*), category
FROM menu
GROUP BY category
HAVING COUNT(*) > ( /* having more than the minimum: */
  SELECT COUNT(*)
  FROM menu
  GROUP BY category
  ORDER BY 1 ASC LIMIT 1 /* smallest category of foods */ 
)
you would not need a subquery to find the information in the subqueries2 example. this is purely for subquery demonstration purposes only.