SQL (Structured Query Language) - Bahasa Kueri Terstruktur: o o o o o o o o o o o o o o
SQL (Structured Query Language) - Bahasa Kueri Terstruktur: o o o o o o o o o o o o o o
SQL OR OPERATOR: Klausa WHERE dapat berisi satu atau lebih operator
OR.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
SELECT *
FROM Customers
WHERE Country = 'Germany' OR Country = 'Spain';
IS NULL Syntax
The IS NULL operator is used to test for empty values (NULL
values).
SELECT column_names
FROM table_name
WHERE column_name IS NULL;
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE Syntax
DELETE FROM table_name WHERE condition;
Delete a Table
DROP TABLE Customers;
LIMIT
The following SQL statement shows the equivalent example for
MySQL:
Example
Select the first 3 records of the Customers table:
FETCH FIRST
The following SQL statement shows the equivalent example for
Oracle:
Example
Select the first 3 records of the Customers table:
Example
SELECT TOP 50 PERCENT * FROM Customers;
Example
SELECT TOP 3 * FROM Customers
WHERE Country='Germany';
SELECT * FROM Customers
WHERE Country='Germany'
LIMIT 3;
Example
Sort the result reverse alphabetically by CustomerName, and return
the first 3 records:
The MAX() function returns the largest value of the selected column.
SELECT MIN/MAX(column_name)
FROM table_name
WHERE condition;
SELECT MIN(Price)// MAX(Price)
FROM Products;
Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT COUNT(*)
FROM Products;
Specify Column
You can specify a column name instead of the asterix symbol (*).
If you specify a column name instead of (*), NULL values will not be
counted.
SELECT COUNT(ProductName)
FROM Products;
Add a WHERE Clause
You can add a WHERE clause to specify conditions:
SELECT COUNT(ProductID)
FROM Products
WHERE Price > 20;
Ignore Duplicates
You can ignore duplicates by using the DISTINCT keyword in
the COUNT() function.
If DISTINCT is specified, rows with the same value for the specified
column will be counted as one.
Example
How many different prices are there in the Products table:
Use an Alias
Give the counted column a name by using the AS keyword.
Example
Name the column "Number of records":
Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Example
Return the sum of all Quantity fields in the OrderDetails table:
SELECT SUM(Quantity)
FROM OrderDetails;
SELECT SUM(Quantity)
FROM OrderDetails
WHERE ProductId = 11;
Use an Alias:
SELECT SUM(Quantity) AS total
FROM OrderDetails;
SUM with GroupBy()
SELECT OrderID, SUM(Quantity) AS [Total Quantity]
FROM OrderDetails
GROUP BY OrderID;
SUM with an EXPRESSIon
The parameter inside the SUM() function can also be an expression.
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT AVG(Price)
FROM Products;
WHERE CategoryID = 1;
Use an alias
Name the column "average price":
SELECT AVG(Price) AS [average price]
FROM Products;
Return all products with a higher price than the average price:
The _ Wildcard
The _ wildcard represents a single character.
The % Wildcard
The % wildcard represents any number of characters, even
zero characters.
Return all customers from a city that contains the letter 'L':
Starts With
To return records that starts with a specific letter or phrase, add
the % at the end of the letter or phrase.
Ends With
To return records that ends with a specific letter or phrase, add
the % at the beginning of the letter or phrase.
Example
Return all customers that ends with 'a':
Contains
To return records that contains a specific letter or phrase, add
the % both before and after the letter or phrase.
Example
Return all customers that contains the phrase 'or'
Combine Wildcards
Any wildcard, like % and _ , can be used in combination with other
wildcards.
Example
Return all customers that starts with "a" and are at least 3
characters in length:
Without Wildcard
If no wildcard is specified, the phrase has to have an exact match to
return a result.
Example
Return all customers from Spain:
# Represents any single numeric 2#5 finds 205, 215, 225, 235, 245,
character 255, 265, 275, 285, and 295
NOT IN
By using the NOT keyword in front of the IN operator, you return all
records that are NOT any of the values in the list.
IN (SELECT)
You can also use IN with a subquery in the WHERE clause.
With a subquery you can return all records from the main query that
are present in the result of the subquery.
Example
Return all customers that have an order in the Orders table:
NOT IN (SELECT)
The result in the example above returned 74 records, that means
that there are 17 customers that haven't placed any orders.
Example
Return all customers that have NOT placed any orders in
the Orders table: