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;
DELETE Syntax
- is used to delete existing records in a table
> DELETE FROM table_name WHERE condition;
SELECT TOP Clause
- is used to specify the number of records to return.
- The SELECT TOP clause is useful on large tables with thousands of records. Returning a large number of records can impact performance.
- **Not all database systems support the SELECT TOP clause. MySQL supports the LIMIT clause to select a limited number of records, while Oracle uses FETCH FIRST n ROWS ONLY and ROWNUM.
Other types of SELECT TOP
1/QL Server / MS Access Syntax:
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
MySQL Syntax:
2/SELECT column_name(s) FROM table_name WHERE condition LIMIT number; Oracle 12 Syntax:
3/SELECT column_name(s) FROM table_name ORDER BY column_name(s) FETCH FIRST number ROWS ONLY; Older Oracle Syntax:
4/SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
Older Oracle Syntax (with ORDER BY):
5/SELECT *
FROM (SELECT column_name(s) FROM table_name ORDER BY column_name(s))
WHERE ROWNUM <= number;
MIN() Syntax
MAC() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
AVG() Syntax
- returns the average value of a numeric column.:
SELECT AVG(column_name)
FROM table_name
WHERE condition;
COUNT() Syntax
- returns the number of rows that matches a specified criterion:
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
- returns the total sum of a numeric column:
SELECT SUM(column_name)
FROM table_name
WHERE condition;
LIKE Operator
- is used in a WHERE clause to search for a specified pattern in a column.
- There are two wildcards often used in conjunction with the LIKE operator:
+ The percent sign (%) represents zero, one, or multiple characters
+ The underscore sign () represents one, single character
**Note: MS Access uses an asterisk () instead of the percent sign (%), and a question mark (?) instead of the underscore ().