SQL
SQL
4. SELECT column1, column2, ... FROM table_name WHERE condition1 AND condition2 AND
condition3 ...;
5. SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR
condition3 ...;
6. SELECT column1, column2, ... FROM table_name WHERE NOT condition;
7. SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ... ASC|
DESC;
8. INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2,
value3, ...);
9. INSERT INTO table_name VALUES (value1, value2, value3, ...);
10. SELECT column_names FROM table_name WHERE column_name IS NULL;
11. SELECT column_names FROM table_name WHERE column_name IS NOT NULL;
12. UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
13. DELETE FROM table_name WHERE condition;
14. DELETE FROM table_name;
15. SELECT TOP number|percent column_name(s) FROM table_name WHERE condition;
16. SELECT TOP 3 * FROM Customers;
17. SELECT * FROM Customers LIMIT 3;
18. SELECT * FROM Customers FETCH FIRST 3 ROWS ONLY;
19. SELECT TOP 50 PERCENT * FROM Customers;
20. SELECT TOP 3 * FROM Customers WHERE Country='Germany';
21. SELECT * FROM Customers WHERE Country='Germany' LIMIT 3;
22. SELECT MIN(column_name) FROM table_name WHERE condition;
23. SELECT MAX(column_name) FROM table_name WHERE condition;
24. SELECT COUNT(column_name) FROM table_name WHERE condition;
25. SELECT AVG(column_name) FROM table_name WHERE condition;
26. SELECT SUM(column_name) FROM table_name WHERE condition;
27. SELECT CustomerName AS Customer, ContactName AS [Contact Person] FROM
Customers;
28. SELECT CustomerName, Address + ', ' + PostalCode + ' ' + City + ', ' + Country
AS Address FROM Customers;
29. The INNER JOIN keyword selects records that have matching values in both
tables.
select Customers.first_name, Customers.last_name, Orders.item from Customers
inner join Orders on Customers.customer_id=Orders.customer_id;
30. The LEFT JOIN keyword returns all records from the left table (table1),
and the matching records from the right table (table2).
The result is 0 records from the right side, if there is no match.
select Customers.first_name, Customers.last_name, Orders.item from Customers
left join Orders on Customers.customer_id=Orders.customer_id;
31. The RIGHT JOIN keyword returns all records from the right table (table2), and
the matching
records from the left table (table1).
The result is 0 records from the left side, if there is no match.
select Customers.first_name, Customers.last_name, Orders.item from Customers
right join Orders on Customers.customer_id=Orders.customer_id;
32. The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or
right (table2) table records.
FULL OUTER JOIN and FULL JOIN are the same.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
33. The UNION operator is used to combine the result-set of two or more SELECT
statements.
Every SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in every SELECT statement must also be in the same order
34. Groupby
The GROUP BY statement groups rows that have the same values into summary rows,
like "find the number of customers in each country
The GROUP BY statement is often used with aggregate functions (COUNT(), MAX(),
MIN(), SUM(), AVG())
to group the result-set by one or more columns.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
36. The EXISTS operator is used to test for the existence of any record in a
subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE
Products.SupplierID = Suppliers.supplierID AND Price < 20);
37. The ANY and ALL operators allow you to perform a comparison between a single
column value
and a range of other values.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ANY
(SELECT column_name FROM table_name WHERE condition);
SELECT ProductName
FROM Products
WHERE ProductID = ANY
(SELECT ProductID FROM OrderDetails WHERE Quantity = 10);
ALL means that the condition will be true only if the operation is true for
all values
in the range.
SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);