Basic SQL Statements Flashcards
What is SQL?
SQL is an ANSI (American National Standard Institute) standard. It is a standard for accessing and manipulating databases.
What does SQL stand for?
Standard Query Language.
What can SQL do?
It can:
- execute queries against a database - retrieve data from a database - insert records in a database - update records in a database - delete records from a database - create new database - create new tables in a database - create stored procedures in a database - create views in a database - set permissions on tables, procedures & views
What is a Database Table?
A database most often contains one or more tables. Each table is identified by a name (e.g. “Customer” or “Order”). Tables contain records (rows) with data.
What is the syntax for the SELECT statement?
SELECT column_name,column_name
FROM table_name;
Ex.
SELECT * FROM table_name;
•This means to show all records table name•
What is the SELECT statement used for?
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
What is the syntax for the SELECT DISTINCT statement?
SELECT DISTINCT column_name,column_name
FROM table_name;
Ex.
SELECT DISTINCT City FROM Customers;
•This means to selects only the distinct values from the “City” columns from the “Customers” table•
What is the SELECT DISTINCT statement used for?
The DISTINCT keyword is used to return only distinct (different) values.
What is the syntax for the WHERE clause statement?
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Ex.
SELECT * FROM Customers
WHERE Country=’Mexico’;
•This means to selects all the customers from the country “Mexico”, in the “Customers” table•
SQL requires single quotes around text values (most database systems will also allow double quotes).
What is the syntax for the WHERE clause statement for numerical field?
SELECT * FROM Customers
WHERE CustomerID=1;
••This means to selects all the customers with 1 for a customer ID in the “Customers” table•
Numeric fields should not be enclosed in quotes.
What are the operators in the WHERE clause?
= Equal
<> Not equal. Note: In some
versions of SQL this operator
may be written as !=
>
Greater than
< Less than
> = Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive
range
LIKE Search for a pattern
IN To specify multiple possible.
values for a column
What is the the statement for the AND operator?
SELECT * FROM Customers
WHERE Country=’Germany’
AND City=’Berlin’;
•This statement selects all customers from the country “Germany” AND the city “Berlin”, in the “Customers” table•
What is the the statement for the OR operator?
SELECT * FROM Customers
WHERE City=’Berlin’
OR City=’München’;
•This statement selects all customers from the city “Berlin” OR “München”, in the “Customers” table•
What is the the statement for the AND & OR operator?
SELECT * FROM Customers
WHERE Country=’Germany’
AND (City=’Berlin’ OR City=’München’);
• This statement selects all customers from the country “Germany” AND the city must be equal to “Berlin” OR “München”, in the “Customers” table•
You can also combine AND and OR (use parenthesis to form complex expressions).
What is the syntax for the ORDER BY keyword statement with the DESC keyword?
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name DESC;
Ex.
SELECT * FROM Customers
ORDER BY Country DESC;
• This statement selects all customers from the “Customers” table, sorted DESCENDING by the “Country” column•