SQL commands Flashcards

1
Q

How to delete multiple rows in SQL where id = (x to y)?

A

DELETE FROM vhd_product WHERE prd_id in (ID#1, ID#2, ID#3);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to join tables?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Applying comments to code

A


/* comment just as in
JavaScript */
Ctrl + / (zamienia zaznaczony obszar) na komentarz

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between HAVING and WHERE

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the options to concatenate

A

You can always join strings in one column using the || character.

SELECT CONCAT(“SQL “, “Tutorial “, “is “, “fun!”) AS ConcatenatedString;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

WHERE operators

A

=
<> lub !

>=, <=
BETWEEN
LIKE
IN
AND
OR
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Show employees that have:
3 letter last name starting with S
OR first_name starting with R

A

SELECT * FROM employees

WHERE last_name LIKE ’S__’ OR first_name LIKE ’R%’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Show records without duplicates

A

SELECT DISTINCT column_name,column_name FROM table_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Substitute a value when a null value is encountered

A

NVL( string1, replace_with )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Date functions to_date, to_char

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Basic grouping functions in PLSQL

A

AVG(), COUNT(), MAX(), MIN(), SUM(), GROUP BY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

INTERSECT

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

MINUS (PLSQL) / EXCEPT

A

Two queries connected by the minus operator will return all lines of the first query that do not appear in the second query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

UNION (ALL)

A

Two queries connected by the union operator will return all elements of both queries.
Duplicated records will be removed (unless we add ALL)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Structure of nested queries

A

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%’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

RPAD function

A

The Oracle/PLSQL RPAD function pads the right-side of a string with a specific set of characters (when string1 is not null).

RPAD(‘tech’, 8, ‘0’)
Result: ‘tech0000’

LPAD works similarly

17
Q

SUBSTR / SUBSTRING function

A

Extract a substring from a string (start at position 1, extract 2 characters):

SELECT SUBSTR(`nazwisko`, 1,2)
FROM `osoby`
18
Q

Creating new tables

A

CREATE TABLE KOPIA_EMPLOYEES AS

select * FROM employees;

19
Q

Deleting tables

A

DROP TABLE KOPIA_EMPLOYEES

20
Q

What is a soft delete?

A

“Soft delete” in database lingo means that you set a flag on an existing table which indicates that a record has been deleted, instead of actually deleting the record.

21
Q

Updating cells in SQL

A

UPDATE vhd_document SET doc_product_ref_id = null WHERE doc_product_ref_id = 43287;