0% found this document useful (0 votes)
2 views

SQL

The document provides a comprehensive overview of SQL commands and their usage, including SELECT, INSERT, UPDATE, DELETE, and JOIN operations. It covers various clauses such as WHERE, GROUP BY, HAVING, and the use of operators like EXISTS, ANY, and ALL. Additionally, it explains the functionality of UNION and different types of joins, along with examples for clarity.

Uploaded by

Abhay Kushwaha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL

The document provides a comprehensive overview of SQL commands and their usage, including SELECT, INSERT, UPDATE, DELETE, and JOIN operations. It covers various clauses such as WHERE, GROUP BY, HAVING, and the use of operators like EXISTS, ANY, and ALL. Additionally, it explains the functionality of UNION and different types of joins, along with examples for clarity.

Uploaded by

Abhay Kushwaha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

SELECT DISTINCT Country FROM Customers;


2.SELECT COUNT(DISTINCT Country) FROM Customers;
3. SELECT column1, column2, ... FROM table_name WHERE condition;
SELECT * FROM Customers WHERE Country='Mexico';
SELECT * FROM Customers WHERE CustomerID=1;
= > < >= <= <> BETWEEN (between 50 and 60) LIKE(like 'S%') IN(in
('Paris','London');)
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any
position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE 'a_%' Finds any values that
start with "a" and are at least 2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that
start with "a" and are at least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends
with "o"

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

SELECT column_name(s) FROM table1


UNION
SELECT column_name(s) FROM table2;

SELECT column_name(s) FROM table1


UNION ALL
SELECT column_name(s) FROM table2;

SELECT City, Country FROM Customers


WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY xyz
ORDER BY City;

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;

35. The HAVING clause was added to SQL


because the WHERE keyword cannot be used with aggregate functions.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

SELECT COUNT(CustomerID), Country


FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;

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.

The ANY operator:


returns a boolean value as a result
returns TRUE if ANY of the subquery values meet the condition
ANY means that the condition will be true if the operation is true for any of
the values
in the
range.

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);

38. The ALL operator:

returns a boolean value as a result


returns TRUE if ALL of the subquery values meet the condition
is used with SELECT, WHERE and HAVING statements

ALL means that the condition will be true only if the operation is true for
all values
in the range.

SELECT ALL column_name(s)


FROM table_name
WHERE condition;

SELECT column_name(s)
FROM table_name
WHERE column_name operator ALL
(SELECT column_name FROM table_name WHERE condition);

You might also like