SQL, Chapter 2: the SELECT Statement
SELECT The SELECT statement fetches data from a database. It has this basic syntax: SELECT [ DISTINCT ] [ * | expression [ [ AS ] output_name ] [, …] ] [ FROM { from_item [ [ AS ] alias … Continued
SELECT The SELECT statement fetches data from a database. It has this basic syntax: SELECT [ DISTINCT ] [ * | expression [ [ AS ] output_name ] [, …] ] [ FROM { from_item [ [ AS ] alias … Continued
To see how the FROM clause and JOIN subclause fit into the SELECT statement, please refer back to Chapter 2. The FROM Clause The FROM clause has this basic syntax: FROM { from_item [ [ AS ] alias ] [, … Continued
where I’ll use these tables in the examples: fruits id name color_id 1 orange 5 2 cherry 1 3 lemon 2 4 grape 6 5 lime 4 6 date null 7 pear 2 colors id color 1 red 2 yellow … Continued
The IN Predicate The IN predicate takes this form: WHERE expression [NOT] IN (list) expression is usually a column, but can also be a calculated value such as column1 + column2. list is a list of scalar values. The WHERE … Continued
exists This chapter covers the WHERE clause’s EXISTS, ANY/SOME, and ALL predicates. The examples in this chapter use these tables: Films id title year duration genre_id 1 Die Hard 1988 132 7 2 Casablanca 1942 102 5 3 The Conversation … Continued
INSERT INSERT is used to create new rows in a table. It has this basic syntax: INSERT INTO table [(column_name [, …])] VALUES { ( expression | DEFAULT [, …] ) | query } ) Where: table is a valid … Continued
DDL commands have to do with working with the database structure. The main commands are CREATE DATABASE, CREATE TABLE, ALTER TABLE, DROP DATABASE and DROP TABLE. CREATE DATABASE CREATE DATABASE creates a new database. It has this basic syntax: CREATE … Continued
This chapter covers some specific constraints that are part of the CREATE TABLE syntax. NOT NULL NOT NULL states that a column requires a value. If an insert or update operation fails to provide a value for the column, it … Continued
Foreign key constraints implement one-to-many relationships. A foreign key is a column in a table where each row matches a primary key value in another table, the foreign table. (Actually, a foreign key can also reference any other column or … Continued
ALTER TABLE alters the definition of an existing table. It uses this basic syntax: ALTER TABLE name { action [, … ] | RENAME TO new_name | RENAME [ COLUMN ] column_name TO new_column_name | RENAME CONSTRAINT constraint_name TO new_constraint_name … Continued