Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

Exercise 40: Reading with SQL

Out of the CRUD matrix you only know "Create". You can create tables and you can create rows in those tables. I'll now show you how to "Read," or in the case of SQL, SELECT:

SELECT * FROM person;

SELECT name, age FROM pet;

SELECT name, age FROM pet WHERE dead = 0;

SELECT * FROM person WHERE first_name != "Zed";

Here's what each of these lines does:

  • ex5.sql:1: This says "select all columns from person and return all rows." The format for SELECT is SELECT what FROM tables(s) WHERE (tests), and the WHERE clause is optional. The '*' (asterisk) character is what says you want all columns.
  • ex5.sql:3: In this one I'm only asking for two columns, name and age, from the pet table. It will return all rows.
  • ex5.sql:5: Now I'm looking for the same columns from the pet table, but I'm asking for only the rows where dead = 0. This gives me all the pets that are alive.
  • ex5.sql:7: Finally I'm selecting all columns from person just like in the first line, but now I'm saying only if they do not equal "Zed". That WHERE clause is what determines which rows to return or not.
Previous Lesson Next Lesson

Register for Learn More Python the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.