SELECT * FROM table1, table2;
Write a query that outputs the name of dogs that either have long fur or are named Grover. SELECT name FROM dogs WHERE fur = 'long' OR name = 'grover'JoinWrite a query that outputs the names and fur types of all of fillmore’s children. SELECT name, fur FROM dogs, parents WHERE parent = 'fillmore' And name = child;Aggregation
SQL provides aggregate functions whose return values can be used as entries in a column. table dogs
out put the average age of all dogs: SELECT AVG(age) AS ave_age FROM dogs;output: output the total number of rows: SELECT COUNT(*) AS count FROM dogs;output: Aggregate function
table dogs
output the sum of ages of all dogs: SELECT SUM(age) AS sum_age FROM dogs;output the name thet comes first alphabetically: SELECT MIN(name) AS min_name FROM dogs;Groups
table dogs
Write a query that finds out the average age of dogs for each fur type. SELECT fur, AVG(age) AS avg_age FROM dogs GROUP BY fur;output:
More on group bySELECT [columns] FROM [table] WHERE [condition] GROUP BY [expression] ORDER BY [order] [ASC/ DESC] LIMIT [number];
Check your understandingtable dogs
table parents
Filtering Groups
table dogs
Write a query that finds the average age of dogs for each fur type if there are more than one dogs with that fur type. SELECT fur, AVG(age) AS avg_age FROM dogs GROUP BY fur HAVING COUNT(*) > 1;output: Check your understandingtable dogs
table parents
Write a query that outputs the average age of each parent’s children if that parent’s youngest child is at least 5. SELECT parent, AVG(age) AS avg_age FROM dogs, parents WHERE name = child GROUP BY parent HAVING MIN(age) >= 5;output:
Mutating TablesDatabases
Create / remove tables
Inserting records
Updating Records
Summary
Summary
|