Notizen zum Einsortieren

This commit is contained in:
Sven Riwoldt
2026-06-17 13:44:55 +02:00
parent 4dde3b8f38
commit cc6ed5796e
+190
View File
@@ -398,6 +398,196 @@ offset 228 --Skipping the first 228 rows in the result set
FETCH NEXT 10 ROWS ONLY;
## Einsortieren
#######################################################
### The AS keyword
Klare und beschreibende Spaltennamen sind essenziell, um Daten auf sinnvolle Weise darzustellen. Wenn Sie eine Tabelle mit schlechten Spaltennamen zeigen, wird es für Ihr Publikum schwer sein zu verstehen, wovon Sie sprechen.
Um Spaltennamen zu ändern, können Sie das AS-Schlüsselwort verwenden.
SELECT col1 AS firstColumn, col2 AS secondColumn, ...
FROM table1
######################################################
## The LIKE keyword
Das LIKE-Schlüsselwort wird verwendet, um Ähnlichkeiten von Zeichenketten zu prüfen. Beispielsweise, wenn wir alle Datensätze abrufen möchten, deren Name mit dem Buchstaben a beginnt, dann verwenden wir das LIKE-Schlüsselwort.
Zwei Hauptwildcards werden verwendet:
% - steht für eine beliebige Anzahl von Zeichen
_ - steht für genau ein Zeichen
Zum Beispiel:
%a - bedeutet jede Zeichenkette, die mit a endet
a% - bedeutet jede Zeichenkette, die mit a beginnt
%a% - bedeutet jeder String, der a enthält
_a% - bedeutet, dass der Buchstabe a der zweite Buchstabe im String ist
%a__ - bedeutet, dass der String a an der drittletzten Stelle enthält
Um es zu verwenden, schreiben wir:
SELECT col1, col2, ...
FROM table1
WHERE col1 LIKE '%a__'
######################################################
To change column names use the AS keyword:
SELECT col1 AS firstColumn, col2 AS secondColumn
FROM table1
The LIKE keyword is used to check string similarities with wildcards:
% - means any number of characters
_ - means exactly one character
Common patterns:
%a - ends with a
a% - starts with a
%a% - contains a
_a% - a is the second character
%a__ - a is in the 3rd from last place
SELECT col1, col2, ...
FROM table1
WHERE col1 LIKE '%a__'
The BETWEEN operator provides a cleaner way to specify ranges instead of using >= and <=:
WHERE col1 BETWEEN 5 AND 10
This is equivalent to:
WHERE col1 >= 5 AND col1 <= 10
The BETWEEN operator is inclusive, meaning it includes the boundary values in the results.
Use IN to check if a column matches any value from a list:
SELECT *
FROM table1
WHERE col1 IN ('a', 'b', 'c', 'd', 'e', 'f')
This is equivalent to multiple OR conditions but much shorter.
To limit the number of records returned by a query, use the LIMIT keyword:
SELECT *
FROM table1
LIMIT 10
This returns the top 10 records from the table.
You can sort data by multiple columns using ORDER BY. The first column specified takes priority, and subsequent columns are used as tie-breakers:
SELECT *
FROM competition
ORDER BY age DESC, avg_speed DESC
This will:
First sort by age in descending order
If records have the same age, then sort by avg_speed in descending order
Use ORDER BY to sort query results. By default, it sorts in ascending order:
SELECT *
FROM table_name
ORDER BY column_name
To specify sort direction, add ASC (ascending) or DESC (descending):
ORDER BY column_name DESC
ORDER BY column_name ASC
Use IS NULL and IS NOT NULL to filter records based on missing values:
SELECT *
FROM table1
WHERE col1 IS NULL
SELECT *
FROM table1
WHERE col1 IS NOT NULL
Boolean is a data type with two possible values: TRUE or FALSE.
Examples:
10 > 100 - FALSE
10 > 5 - TRUE
10 > 5 AND 100 < 5 - FALSE
Internally, TRUE is represented as 1 and FALSE is represented as 0.
To filter data using booleans, use IS TRUE or IS NOT TRUE keywords:
SELECT *
FROM table1
WHERE col1 IS NOT FALSE AND col2 IS TRUE
Use parentheses to control the order of operations when combining multiple conditions with AND and OR:
WHERE age < 30 OR gender = 'female' AND age > 20
Returns all people under 30 (any gender) plus all females over 20.
WHERE (age < 30 OR gender = 'female') AND age > 20
Returns only people over 20 who are either under 30 or female.
Use AND, OR, and NOT keywords to create complex conditions in SQL queries:
SELECT col1, col2
FROM table1
WHERE condition1 AND condition2 OR condition3
You can stack as many conditions as needed together using these logical operators.
The NOT keyword is used to negate a condition, meaning we don't want the condition to be met.
SELECT *
FROM people
WHERE NOT gender = 'male'
The NOT keyword flips the condition. These queries are equivalent:
WHERE age > 20
WHERE NOT age <= 20
The OR keyword returns records where at least one condition is true:
SELECT *
FROM people
WHERE gender = 'female' OR age < 20
Returns rows where gender = 'female' or age < 20 (or both).
The AND keyword requires both conditions to be true:
SELECT *
FROM people
WHERE gender = "female" AND age < 20
This returns records where the person is female AND under 20 years old.
Use the WHERE keyword to add conditions to your SQL queries:
SELECT * FROM table_name
WHERE column_name = 'value'
For numeric comparisons:
SELECT * FROM table_name
WHERE column_name <= 20
Common comparison operators: =, <, >, <=, >=
Use DISTINCT to get unique values from a column:
SELECT DISTINCT column_name FROM table_name
Without DISTINCT, duplicate values will be returned. With DISTINCT, each unique value appears only once in the result.
In databases, rows are called records and columns are called fields.
Use the asterisk * symbol to select all columns from a table:
SELECT * FROM table_name
Databases store data in organized tables with columns and rows. SQL (Structured Query Language) is used to manage and manipulate data in databases.
To extract data from a database table, use the SELECT statement with the FROM clause:
SELECT column1, column2, column3 FROM table_name
#####################################################
#### Notizen bei Fehlersuche
Hier sollen Werte aus eriner Tabelle in eine andere transferiert werden, jedoch es knallt: