Sample Video Frame

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

Exercise 5: Selecting Data

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:

What You Should See

When you run this with sqlite3 -echo you should get something like the following output:

$ sqlite3 -echo ex3.db < ex5.sql 
SELECT * FROM person;
SELECT name, age FROM pet;
SELECT name, age FROM pet WHERE dead = 0;
SELECT * FROM person WHERE first_name != "Zed";

I say "something like" because if you were doing the extra credit this whole time you will have different rows in your database. For example, if you added yourself then you will have some rows listed at the end. In my example above I have nothing returned for the last query because I'm the only person in the person table, and that means no row match the last query's WHERE clause. Study this carefully.

Study Drills

Portability Notes

Some databases have additional operators and boolean logic tests, but just stick to the regular ones that you find in most programming languages for now.

Previous Lesson Next Lesson

Register for Learn SQL 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.