Sie sind auf Seite 1von 4

SQL BASICS

SELECT * FROM database ; Selects whole database

SELECT COUNT/AVG/SUM Counts/averages/sums up


(column_name) total from table
FROM table_name
WHERE condition;

SELECT DISTINCT column1, Used to return only SELECT DISTINCT Country FROM Customers;
column2, distinct (different)
FROM table_name; values

SELECT column1, column2, ... WHERE clause is used to SELECT * FROM Customers
FROM table_name extract only those WHERE Country='Mexico';
WHERE condition; records that fulfill a
specified condition

SELECT column1, column2, ... WHERE clause can be SELECT * FROM Customers
FROM table_name combined with AND, OR, WHERE Country='Germany' AND City='Berlin';
WHERE condition1 AND/NOT/OR and NOT operators.
condition2 AND/NOT/OR condition3 SELECT * FROM Customers
...; The AND and OR operators WHERE Country='Germany' AND (City='Berlin'
are used to filter OR City='München');
records based on more
than one condition

DELETE FROM table_name ORDER BY keyword sorts DELETE FROM Customers


WHERE condition; the records in ascending WHERE CustomerName='Alfreds Futterkiste';
order by default. To
sort the records in
descending order, use
the DESC keyword.
SELECT MIN/MAX (column_name) Min = returns smallest SELECT MIN(Price) AS SmallestPrice
FROM table_name value FROM Products;
WHERE condition; Max = returns largest
value
INSERT INTO table_name (column1, It is also possible to INSERT INTO Customers (CustomerName, City,
column2, column3, ...) only insert data in Country)
VALUES (value1, value2, value3, specific columns. VALUES ('Cardinal', 'Stavanger',
...); 'Norway');
Will insert a new
record, but only insert
data in the
"CustomerName", "City",
and "Country" columns
(CustomerID will be
updated automatically):
SELECT column_names It is not possible to SELECT LastName, FirstName, Address FROM
FROM table_name test for NULL values Persons
WHERE column_name IS NULL / NOT with comparison WHERE Address IS NULL;
NULL; operators, such as =, <,
or <>.

NULL or NOT NULL


UPDATE table_name Updates the first UPDATE Customers
SET column1 = value1, column2 = customer (CustomerID = SET ContactName = 'Alfred Schmidt', City=
value2, ... 1) with a new contact 'Frankfurt'
WHERE condition; person and a new city. WHERE CustomerID = 1;

DELETE FROM table_name Used to delete existing DELETE FROM Customers


WHERE condition; records in table WHERE CustomerName='Alfreds Futterkiste';
SELECT column_name(s) Used to specify number SELECT * FROM Customers
FROM table_name of records to return. WHERE ROWNUM <= 3;
WHERE ROWNUM <= number;
Useful on large tables
incase impacts on
performance
SELECT column1, column2, ... The LIKE operator is SELECT * FROM Customers
FROM table_name used in a WHERE clause WHERE CustomerName LIKE '%or%';
WHERE columnN LIKE pattern; to search for a
specified pattern in a
column.
WHERE
Finds any values that
CustomerName
There are two wildcards starts with "a"
LIKE 'a%'
used in conjunction with
the LIKE operator:

 % - The percent WHERE


Finds any values that
sign represents CustomerName
ends with "a"
zero, one, or LIKE '%a'
multiple
characters
 _ - The
underscore WHERE
Finds any values that
represents a CustomerName
have "or" in any position
single character LIKE '%or%'

WHERE Finds any values that


CustomerName have "r" in the second
LIKE '_r%' position

Finds any values that


WHERE
starts with "a" and are
CustomerName
at least 3 characters in
LIKE 'a_%_%'
length

WHERE Finds any values that


ContactName LIKE starts with "a" and ends
'a%o' with "o"

SELECT column_name(s) SELECT * FROM Customers


FROM table_name WHERE Country IN ('Germany', 'France',
'UK');
WHERE column_name IN (value1,
value2, ...); Or

Or WHERE Country IN (SELECT Country FROM


Suppliers);
WHERE column_name IN (SELECT
STATEMENT);
SELECT column_name(s) The BETWEEN operator SELECT * FROM Products
FROM table_name selects values within a WHERE Price BETWEEN 10 AND 20;
WHERE column_name BETWEEN value1 given range. The values
AND value2; can be numbers, text, or
dates.
SELECT column_name AS alias_name SQL aliases are used to SELECT CustomerID as ID, CustomerName AS
FROM table_name; give a table, or a Customer
column in a table, a FROM Customers;
SELECT column_name(s) temporary name.
FROM table_name AS alias_name;
Aliases are often used
to make column names
more readable.

SELECT column_name(s) FROM JOINS values from two or SELECT City FROM Customers
table1 more tables UNION ALL
UNION SELECT City FROM Suppliers
SELECT column_name(s) FROM ORDER BY City;
table2;
SELECT column_name(s) The GROUP BY statement SELECT COUNT(CustomerID), Country
FROM table_name is often used with FROM Customers
WHERE condition aggregate functions GROUP BY Country;
GROUP BY column_name(s) (COUNT, MAX, MIN, SUM,
ORDER BY column_name(s); AVG) to group the
result-set by one or
more columns.

SELECT column_name(s) The EXISTS operator is SELECT SupplierName


FROM table_name used to test for the FROM Suppliers
WHERE EXISTS existence of any record WHERE EXISTS (SELECT ProductName FROM
(SELECT column_name FROM in a subquery. Products WHERE SupplierId =
table_name WHERE condition); Suppliers.supplierId AND Price < 20);
The EXISTS operator
returns true if the
subquery returns one or
more records.

SELECT column_name(s) The ANY and ALL SELECT ProductName


FROM table_name operators are used with FROM Products
WHERE column_name operator a WHERE or HAVING WHERE ProductID = ANY (SELECT ProductID
ANY/ALL clause. FROM OrderDetails WHERE Quantity = 10);
(SELECT column_name FROM
table_name WHERE condition); The ANY operator returns
true if any of the
subquery values meet the
condition.

The ALL operator returns


true if all of the
subquery values meet the
condition.

SELECT * The SELECT INTO SELECT CustomerName, ContactName INTO


Or statement copies data CustomersBackup2017
SELECT column1, column2, from one table into a FROM Customers;
column3, ... new table.
INTO newtable [IN externaldb]
FROM oldtable
WHERE condition;

INSERT INTO table2 The INSERT INTO SELECT INSERT INTO Customers (CustomerName, City,
SELECT * FROM table1 statement copies data Country)
WHERE condition; from one table and SELECT SupplierName, City, Country FROM
inserts it into another Suppliers;
table.
 INSERT INTO
SELECT requires
that data types
in source and
target tables
match
 The existing
records in the
target table are
unaffected

Left Join:

Returns only unmatched


rows from left table
Right Join:

Returns only unmatched


rows from the right
table.
SELECT column_name(s) Full Outer Join:
FROM table1
FULL OUTER JOIN table2 ON Returns all record when
there is a match in
table1.column_name = either table 1 or table
table2.column_name; 2 records.

Das könnte Ihnen auch gefallen