We often select only a portion of out huge database. For example, we will use table from last article and we would like to select only students with bikes. Let’s do it.
SELECT id FROM students WHERE has_bicycle = true;
Examples of another where clause uses:
SELECT * FROM students WHERE surname = 'Smith';
SELECT * FROM students WHERE id = 1;
SELECT * FROM students WHERE date_of_birth < '2005/01/01';
SQL (Structured Query Language ) is language created for querying from database.
Assume you have a database. You want to insert new stuff, update it, select some stuff or maybe delete it overtime.
Easy explained, the database consists of several tables, for example table of students:
The first column is unique id of record in the table (it is not number of the row as we can delete second row and row with id 3 will be second then).
Let’s start with selection. The basic syntax is:
/* comment in sql */
/* basic syntax ended with ; */ SELECT columns FROM table;
/* examples */
/* 1 */ SELECT id FROM students;
/* 2 */ SELECT name, surname FROM students;
/* 3 */ SELECT id, name, surname, date_of_birth, has_bicycle FROM students;
/* 4 */ SELECT * FROM students;
The first query SELECT id FROM students will return whole column with ids. The last 2 queries are the same, * character means everything (every column). The result of query number 2 will be: