Pro lang Flashcards
SQL
- Structured Query Language
- SQL lets you access and manipulate databases
Basic Queries(in SQL)
- Querying data: SELECT
- Sorting data: ORDER BY
- Filtering data: WHERE, AND, OR, IN, BETWEEN, LIKE, IS NULL
SQL SELECT
- SELECT statement to retrieve data from all columns example
SELECT lastname, firstname, jobtitle
FROM employees;
ORDER BY(SQL)
SELECT select_list
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], …;
Sort values in multiple columns (SQL)
SELECT contactLastname, contactFirstname
FROM customers
ORDER BY contactLastname, contactFirstname;
Sort a result set by an expression example
SELECT orderNumber, orderLineNumber, quantityOrdered * priceEach AS subtotal
FROM orderdetails
ORDER BY subtotal DESC;
WHERE clause(SQL)
- SELECT select_list FROM table_name WHERE search_condition; - VD: SELECT lastname, firstname, jobtitle FROM employees WHERE jobtitle = 'Sales Rep';
- The search_condition is a combination of one or more predicates using the logical operator AND, OR and NOT
WHERE clause with AND operator(SQL)
SELECT lastname, firstname, jobtitle, officeCode
FROM employees
WHERE jobtitle = ‘Sales Rep’ AND officeCode = 1;
WHERE clause with comparison operators(SQL)
= : equal to
AND, OR and NOT Operators(SQL)
- 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 Syntax(SQL)
SELECT column1, column2, …
FROM table_name
WHERE condition1 AND condition2 AND condition3 …;
Vd:
SELECT * FROM Customers
WHERE Country=’Germany’ AND City=’Berlin’;
OR Syntax(SQL)
SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...; Vd: SELECT * FROM Customers WHERE City='Berlin' OR City='München';
NOT Syntax
SELECT column1, column2, ... FROM table_name WHERE NOT condition; Vd: SELECT * FROM Customers WHERE Country='Germany' OR Country='Spain';
Combining AND, OR and NOT
Vd1:
SELECT * FROM Customers
WHERE Country=’Germany’ AND (City=’Berlin’ OR City=’München’);
Vd2:
SELECT * FROM Customers
WHERE NOT Country=’Germany’ AND NOT Country=’USA’;
INSERT INTO Statement(SQL)
is used to insert new records in a table
INSERT INTO Syntax
S1:
Specify both the column names and the values to be inserted:
> INSERT INTO table_name (column1, column2, column3, …)
VALUES (value1, value2, value3, …);
S2:
> If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table. Here, the INSERT INTO syntax would be as follows:
> INSERT INTO table_name
VALUES (value1, value2, value3, …);
SELECT DISTINCT statement
is used to return only distinct (different) values.
SELECT DISTINCT Syntax
SELECT DISTINCT column1, column2, …
FROM table_name;
SELECT COUNT(DISTINCT Country) FROM Customers;
lists the number of different (distinct) customer countries:
a NULL Value
is a field with no value.
How to Test for NULL Values?
It is not possible to test for NULL values with comparison operators, such as =, .
We will have to use the IS NULL and IS NOT NULL operators instead
(NOT) IS NULL Syntax
SELECT column_names
FROM table_name
WHERE column_name (NOT) IS NULL
UPDATE Statement
is used to modify the existing records in a table
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;