Error
Errors:
SQL Documentation
Selecting from a table.
SELECT * FROM Table;
Selecting only certain fields from a table.
SELECT field1, field2 FROM Table;
WHERE clauses.
-- Basic WHERE clause SELECT * FROM Table WHERE condition; -- Compound WHERE clauses SELECT * FROM Table WHERE condition1 AND condition2;
Condition operators
= equal < less than > greater than <> not equal IN (a, b, c) Value is in a list BETWEEN x AND y Value is between x and y LIKE "%" String matches a pattern
String patterns.
"A%" (words starting with "A") "%a" (words ending with "a") "%a%" (words with "a" in it)
Ordering / Sorting queries.
// Sort by a field name ORDER BY field_name; -- Sort by a field name in reverse ORDER BY field_name DESC; -- Sort by a field and then sort by a 2nd field when the 1st field is equal ORDER BY field_1, field_2;
Renaming fields in results
SELECT field_name AS "Field Name" SELECT first_name AS "First Name"
Joining Tables.
-- Select all fields in both tables combined. SELECT * FROM TableA JOIN TableB; -- Select only the rows in the joined table where the ids match SELECT * FROM TableA JOIN TableB WHERE TableA.id = TableB.id; -- Select only the relevant columns from each table -- Where their ids match SELECT TableA.value, TableB.value FROM TableA JOIN TableB WHERE TableA.id = TableB.id;
The COUNT Function
-- Return the number of rows in Table SELECT COUNT(*) FROM Table; -- Return the number of houses and name -- the column "Num Houses" SELECT COUNT(*) As "Num Houses" FROM House;
Grouping
-- Syntax for grouping and counting SELECT field_name, COUNT(*) FROM Table GROUP BY field_name;
Filtering with HAVING
-- You use HAVING instead of WHERE if you are -- filtering after a GROUP BY SELECT field_name, COUNT(*) FROM Table GROUP BY field_name HAVING condition;
Limiting results
-- Use LIMIT after any query to limit the number of results SELECT * FROM Table LIMIT 10; -- Ex) Show the 5 highest values only SELECT field_name, COUNT(*) FROM Table GROUP BY field_name ORDER BY COUNT(*) DESC LIMIT 5;
Slides and Notes
No slides available for this video
Collaborate on this sandbox program!
Admin Only. Not visible to customers
A collaborative program is a program you can work on with a partner or group of people. The program shows up in all of your sandbox pages, and when any of you save code, it will save it for each person.
Current Collaborators:
Embed Your Code On an HTML Page
Want to use your CodeHS code outside of CodeHS? Use this embed code to get started.
How to Use
Version 1: On Your Computer
- Create a file called
codehs.html
on your desktop - Put the html below into the codehs.html file and save it
- Now double click the file to open it in a web browser
Version 2: Online
- Create a file called
codehs.html
on your desktop - Put the html below into the codehs.html file and save it
- Upload this file to the internet
- Visit your web page