SQL commands Flashcards
How to delete multiple rows in SQL where id = (x to y)?
DELETE FROM vhd_product WHERE prd_id in (ID#1, ID#2, ID#3);
How to join tables?
SELECT t.rtf_field_id, d.fdf_name, d.fdf_type FROM vhd_report template_field t JOIN vhd_field_definition d ON t.rtf_field_id = d.fdf_id WHERE t.rtf_field_id = 1102
Applying comments to code
–
/* comment just as in
JavaScript */
Ctrl + / (zamienia zaznaczony obszar) na komentarz
What is the difference between HAVING and WHERE
The main difference between WHERE and HAVING clause comes when used together with GROUP BY clause, In that case WHERE is used to filter rows before grouping and HAVING is used to exclude records after grouping.
One applies before and one after operations on an aggregated data set
What are the options to concatenate
You can always join strings in one column using the || character.
SELECT CONCAT(“SQL “, “Tutorial “, “is “, “fun!”) AS ConcatenatedString;
WHERE operators
=
<> lub !
>=, <= BETWEEN LIKE IN AND OR
Show employees that have:
3 letter last name starting with S
OR first_name starting with R
SELECT * FROM employees
WHERE last_name LIKE ’S__’ OR first_name LIKE ’R%’;
Show records without duplicates
SELECT DISTINCT column_name,column_name FROM table_name;
Substitute a value when a null value is encountered
NVL( string1, replace_with )
Date functions to_date, to_char
The Oracle/PLSQL TO_DATE function converts a string to a date.
The Oracle/PLSQL TO_CHAR function converts a number or date to a string.
Basic grouping functions in PLSQL
AVG(), COUNT(), MAX(), MIN(), SUM(), GROUP BY
INTERSECT
The SQL INTERSECT operator is used to return the results of 2 or more SELECT statements. However, it only returns the rows selected by all queries or data sets. If a record exists in one query and not in the other, it will be omitted from the INTERSECT results.
MINUS (PLSQL) / EXCEPT
Two queries connected by the minus operator will return all lines of the first query that do not appear in the second query.
UNION (ALL)
Two queries connected by the union operator will return all elements of both queries.
Duplicated records will be removed (unless we add ALL)
Structure of nested queries
SELECT e.first_name, e.last_name, e.salary, d.department_name
FROM employees e
JOIN departments d
ON e.employee_id = d.manager_id
WHERE e.salary < (SELECT MEDIAN(salary)
FROM employees e
JOIN departments d
ON e.employee_id = d.manager_id)
AND e.last_name like ‘H%’;