Spreadsheets break at scale
A spreadsheet is also rows and columns, so why bother with a database? Because a spreadsheet is a picture of data you edit by hand, while a database is a service that programs talk to. The differences show up fast:
- Size. A spreadsheet slows to a crawl around a hundred thousand rows. Databases handle billions.
- Many users at once. Two people editing one file overwrite each other. A database safely handles thousands of simultaneous readers and writers.
- Rules. Nothing stops a spreadsheet cell from holding
"three"where an age should be. A database can refuse bad data (you will set these rules up in unit 7). - Questions. In SQL, "average age of every dog owned by users who signed up this year" is one query. In a spreadsheet it is an afternoon of clicking.
Plain files (like .txt or .csv) have the same problems, plus you would have to write all the searching code yourself.
Quiz
Your app has 40 million users and 2,000 of them are saving changes at the same moment. Which spreadsheet weakness matters most here?
Asking for specific columns
SELECT * grabs every column, but usually you only want some. List the column names you want, separated by commas:
SELECT name, age FROM pets;
Read it right to left: from the pets table, select the name and age columns. The columns come back in the order you list them, and every row is still included. Choosing columns never removes rows, it only trims what each row shows.
Code exercise · sql
Run it. Same three pets, but each output row now shows only name and age, so there is just one | per line.
Code exercise · sql
Your turn. Change the SELECT so it prints only the species column. Each output line should be a single word.
Problem
You want each pet's species and its name, in that order, from the pets table. Write the full SQL query (end it with a semicolon).