{"base":{"id":"cs10-26","subject":"Databases","title":"Asking questions with SQL","scene":"mix","steps":[{"use":"cs10t3","mode":"sql","eyebrow":"SELECT and WHERE","title":"Choose fields and records","body":"SQL asks a database questions. SELECT picks fields, FROM names the table, WHERE picks records.","actions":[{"id":"q0","label":"Query 1"}]},{"use":"cs10t3","mode":"sql","eyebrow":"AND, OR and ORDER BY","title":"Combine and sort","body":"AND needs both conditions; OR needs either. ORDER BY sorts the results. Try each query.","actions":[{"id":"q1","label":"AND"},{"id":"q2","label":"ORDER BY"}]},{"use":"cs10t3","mode":"sql","eyebrow":"COUNT and SUM","title":"One answer from many","body":"Aggregate functions summarise: COUNT counts records, SUM adds a field, AVG finds the mean, MAX and MIN find extremes.","actions":[{"id":"q3","label":"COUNT and MAX"}]},{"use":"data9","mode":"qtext","eyebrow":"From words to SQL","title":"Turn a question into conditions","body":"Read a question carefully and turn each part into a condition.","actions":[{"id":"q0","label":"Question 1"},{"id":"q1","label":"Question 2"},{"id":"q2","label":"Question 3"}]},{"use":"cs10t3","mode":"quiz","look":"sql","eyebrow":"Check yourself","title":"Read a query","body":"SELECT Name FROM Planets WHERE Diameter > 50000;","question":"How many names does it return?","options":["4","3","2"],"answer":1,"right":"Correct. Jupiter, Saturn and Uranus. Neptune is 49 528 km, just under.","wrong":"Not quite. Check Neptune's diameter carefully."}],"more":[{"type":"cards","id":"worked","eyebrow":"Worked examples","title":"Step by step","items":[{"tag":"Basic","title":"Fields and condition","body":"SELECT Title, Year\nFROM Films\nWHERE Year >= 2000;","code":true},{"tag":"Text","title":"Quotes around text","body":"SELECT * FROM Films\nWHERE Genre = 'Comedy';","code":true},{"tag":"Sort","title":"Highest first","body":"SELECT Title, Rating\nFROM Films\nORDER BY Rating DESC;","code":true},{"tag":"Aggregate","title":"Count and average","body":"SELECT COUNT(*), AVG(RunTime)\nFROM Films\nWHERE Genre = 'Animation';","code":true}]},{"type":"cards","id":"mistakes","eyebrow":"Common mistakes","title":"Watch out for these","items":[{"tag":"Mistake","title":"Forgetting quotes on text","body":"WHERE Name = Earth fails; write 'Earth'."},{"tag":"Mistake","title":"Using AND when you mean OR","body":"\"Comedy or Drama\" needs OR; a film can't be both genres in one field."},{"tag":"Mistake","title":"Missing the semicolon","body":"End each statement with ; in most systems."},{"tag":"Mistake","title":"Writing DESC on the wrong part","body":"DESC goes after the field in ORDER BY."}]},{"type":"cards","id":"deeper","eyebrow":"Go deeper","title":"More than the lesson","items":[{"tag":"Real world","title":"SQL is everywhere","body":"SQL was created at IBM in the 1970s and is still one of the most used programming languages. Apps on your phone often store data in a small SQL database called SQLite."},{"tag":"Security","title":"SQL injection","body":"If a website pastes user input straight into SQL, an attacker can type SQL code into a form. Programmers prevent this with parameterised queries."},{"tag":"Going further","title":"JOIN","body":"JOIN combines rows from two tables using a foreign key, so you can ask \"which members borrowed which books?\""}]},{"type":"cards","id":"challenge","eyebrow":"Challenge yourself","title":"Stretch your brain","intro":"Try each one on paper before opening the card.","items":[{"tag":"Challenge","title":"Write","body":"Write SQL to list the names of rocky planets in alphabetical order.\n\nAnswer:\nSELECT Name FROM Planets\nWHERE Rocky = TRUE\nORDER BY Name;","code":true},{"tag":"Challenge","title":"Predict","body":"SELECT COUNT(*) FROM Planets WHERE DayHours < 20; Result?\n\nAnswer: 4 (Jupiter, Saturn, Uranus, Neptune)."},{"tag":"Challenge","title":"Fix","body":"Find the error: SELECT Name FROM Planets WHERE Name = Mars\n\nAnswer: Mars needs quotes: 'Mars', and add a semicolon."}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Try it yourself","items":[{"title":"Query a real database (free, online)","time":"20 minutes","need":["A web browser","A free online SQL sandbox (search \"SQLite online\")"],"steps":["Create a small table of 5 favourite films with title, year and rating.","Write a SELECT with WHERE to list films after 2015.","Sort them by rating."],"why":"You wrote and ran real SQL.","safety":"Don't enter personal information into online tools."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Eight more questions","questions":[{"question":"SELECT chooses…","options":["tables to delete","records to update","fields"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"WHERE chooses…","options":["which records","which table","the sort order"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"ORDER BY Score DESC sorts…","options":["lowest first","highest first","alphabetically"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"COUNT(*) returns…","options":["the total of a field","the largest value","the number of matching records"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"SELECT * means…","options":["all fields","all tables","no fields"],"answer":0,"right":"Correct.","wrong":"Not quite."},{"question":"Which needs quotes?","options":["42","'Paris'","TRUE"],"answer":1,"right":"Correct.","wrong":"Not quite."},{"question":"SUM(Price) gives…","options":["the number of prices","the mean price","the total of all prices"],"answer":2,"right":"Correct.","wrong":"Not quite."},{"question":"AND in WHERE means…","options":["both conditions true","either condition true","neither"],"answer":0,"right":"Correct.","wrong":"Not quite."}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can…","items":["write SELECT … FROM … WHERE queries","use comparison operators and text in quotes","combine conditions with AND and OR","sort results with ORDER BY ASC/DESC","use COUNT, SUM, AVG, MAX and MIN","predict the output of a query"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"SQL","meaning":"Structured Query Language, used to work with databases."},{"word":"Query","meaning":"A question asked of a database."},{"word":"SELECT","meaning":"Chooses the fields to show."},{"word":"WHERE","meaning":"Filters records by a condition."},{"word":"ORDER BY","meaning":"Sorts the results."},{"word":"Aggregate function","meaning":"A function that summarises many records, like COUNT."}]},{"type":"sources","id":"sources","title":"Sources","items":["SQLite documentation: SELECT","NASA Planetary Fact Sheet (diameters and rotation periods)","OWASP: SQL Injection Prevention Cheat Sheet","Raspberry Pi Foundation, Isaac Computer Science: SQL"]}]},"overlay":null}