SQL Syntax Flashcards
SELECT
extracts data from a database
UPDATE
updates data in a database
DELETE
deletes data from a database
INSERT INTO
inserts new data into a database
CREATE DATABASE
creates a new database
ALTER DATABASE
modifies a database
CREATE TABLE
creates a new table
ALTER TABLE
modifies a table
DROP TABLE
deletes a table
CREATE INDEX
creates an index (search key)
DROP INDEX
deletes an index
SELECT *
selects all the columns from the specific table
SELECT DISTINCT
select all the different values from the specific columns in specific table,
a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values. NOT LIST DUPLICATE
SELECT COUNT
lists the number of different (distinct) countries in table ,,,doesNOT work for firefox, NOT supported for MS access databases.
SELECT WHERE
is used to filter records. is used to extract only those records that fulfill a specified condition. USED in SELECT, UPDATE , DELETE statements.
TEXT FIELDS VS NUMBERIC FIELDS
TEXT=require single quotes around text values(most database systems will also allow double quotes).
NUMERIC= should NOT be enclosed in quotes
Operators in the WHERE Clause
= Equal > Greater than < Less than >= Greater than or equal <= Less than or equal <> Not equal. Note: In some versions of SQL this operator may be written as !=
BETWEEN Between a certain range
LIKE Search for a pattern
IN To specify multiple possible values for a column
The SQL AND , OR and NOT Operators
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
The AND operator displays a record if all the conditions separated by AND are TRUE.
The OR operator displays a record if any of the conditions separated by OR is TRUE.
The NOT operator displays a record if the condition(s) is NOT TRUE.
AND Syntad
SELECT column1, column2, …
FROM table_name
WHERE condition1 AND condition2 AND condition3 …;
all conditions must be true
OR Syntax
SELECT column1, column2, …
FROM table_name
WHERE condition1 OR condition2 OR condition3 …;
atleast one condition must be true
NOT Syntax
SELECT column1, column2, …
FROM table_name
WHERE NOT condition;
no condition must be true to display
Combining AND, OR and NOT
You can also combine the AND, OR and NOT operators.
The following SQL statement selects all fields from “Customers” where country is “Germany” AND city must be “Berlin” OR “München” (use parenthesis to form complex expressions):
SELECT * FROM Customers
WHERE Country=’Germany’ AND (City=’Berlin’ OR City=’München’);
The following SQL statement selects all fields from “Customers” where country is NOT “Germany” and NOT “USA”:
Example
SELECT * FROM Customers
WHERE NOT Country=’Germany’ AND NOT Country=’USA’;
SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
SELECT column1, column2, …
FROM table_name
ORDER BY column1, column2, … ASC|DESC;
SEVERAL COLUMNS
SELECT * FROM Customers
ORDER BY Country, CustomerName;
ASC and DESC
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;