SQL1
SQL1
SQL Distinct
SQL Where
SQL AND, OR and NOT Operators
SQL INSERT INTO Statement
SQL NULL Values
SQL UPDATE Statement
o UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The SQL DELETE Statement
o To delete all records from tables-
DELETE FROM Customers;
SQL TOP, LIMIT, FETCH FIRST or ROWNUM Clause
o select top 5 name from emp
o SELECT TOP 50 PERCENT * FROM Customers;
SQL MIN() and MAX() Functions
The SQL LIKE Operator
The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
There are two wildcards often used in conjunction with the LIKE operator:
o The percent sign (%) represents zero, one, or multiple characters
o The underscore sign (_) represents one, single character
SQL Wildcards
% Represents zero or more characters bl% finds bl, black, blue, and blob
[] Represents any single character within h[oa]t finds hot and hat, but not hit
the brackets
^ Represents any character not in the h[^oa]t finds hit, but not hot and hat
brackets
- Represents any single character within c[a-b]t finds cat and cbt
the specified range
SQL In
The IN operator is a shorthand for multiple OR conditions.
Example
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
SQL BETWEEN Operator
SELECT * FROM Products
WHERE Price BETWEEN 10 AND 20;
SELECT * FROM Products
WHERE Price NOT BETWEEN 10 AND 20;
SQL Aliases
SELECT CustomerName, CONCAT(Address,', ',PostalCode,', ',City,',
',Country) AS Address FROM Customers;
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
Notice that the "CustomerID" column in the "Orders" table refers to the
"CustomerID" in the "Customers" table. The relationship between the two
tables above is the "CustomerID" column.
Then, we can create the following SQL statement (that contains an INNER
JOIN), that selects records that have matching values in both tables:
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 column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;
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 column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
SELECT A.CustomerName AS CustomerName1,
B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
https://www.youtube.com/watch?v=0OQJDd3QqQM
https://www.youtube.com/watch?v=RehbnzKHS28
Natural Join
Self Join
Child.age as child-age,
parent.name as parent_name,
parent.age as parent_age
on child.parent_id=parent.member_id
SQL Union-
SELECT City FROM Customers
UNION
SELECT City FROM Suppliers
ORDER BY City;
SQL Having
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
EXEC SelectAllCustomers;
BACKUP DATABASE databasename
TO DISK = ' E:\DS_Preperation\abc.bak';