Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

SQL1

This document provides an overview of various SQL statements and concepts including SELECT statements, WHERE clauses, JOINs, functions, wildcards and more. Key topics covered include SQL SELECT, WHERE, JOIN, UPDATE, DELETE statements; operators like AND, OR, NOT, BETWEEN; aggregate functions like COUNT, MIN, MAX; and table altering commands.

Uploaded by

Mahendra Ugale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

SQL1

This document provides an overview of various SQL statements and concepts including SELECT statements, WHERE clauses, JOINs, functions, wildcards and more. Key topics covered include SQL SELECT, WHERE, JOIN, UPDATE, DELETE statements; operators like AND, OR, NOT, BETWEEN; aggregate functions like COUNT, MIN, MAX; and table altering commands.

Uploaded by

Mahendra Ugale
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

 SQL Select

 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

Symbol Description Example

% Represents zero or more characters bl% finds bl, black, blue, and blob

_ Represents a single character h_t finds hot, hat, and hit

[] 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.

Let's look at a selection from the "Orders" table:

OrderID CustomerID OrderDate


10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20

Then, look at a selection from the "Customers" table:

CustomerID CustomerName ContactName Country

1 Alfreds Futterkiste Maria Anders Germany

2 Ana Trujillo Emparedados y Ana Trujillo Mexico


helados

3 Antonio Moreno Taquería Antonio Moreno Mexico

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:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
SQL LEFT JOIN Keyword

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;

SQL RIGHT JOIN Keyword

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;

SELECT Orders.OrderID, Employees.LastName, Employees.FirstName


FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;

SQL FULL OUTER JOIN Keyword

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;

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;

SQL Self Join


A self join is a regular join, but the table is joined with itself.

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

SQL INNER JOIN Keyword

SQL LEFT JOIN Keyword

SQL RIGHT JOIN

SQL FULL OUTER JOIN/FULL JOIN

SQL CROSS JOIN- Cartesian Product

Natural Join

 Not similar to inner join having some differences


 No need to specify “ON”.
 If we change column name then it performs cross join or Cartesian
product.
 Using natural join is not recommended

Self Join

Select child.name as child_name,

Child.age as child-age,

parent.name as parent_name,

parent.age as parent_age

from family as child

join family as parent

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;

CREATE PROCEDURE SelectAllCustomers


AS
SELECT * FROM employee
GO;

EXEC SelectAllCustomers;

CREATE PROCEDURE SelectE @emp nvarchar(30)


AS
SELECT * FROM employee WHERE emp_name = @emp
GO

EXEC SelectE @emp='Rahul'

CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode


nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;

EXEC SelectAllCustomers @City = 'London', @PostalCode = 'WA1 1DP';

The SQL BACKUP DATABASE Statement

BACKUP DATABASE databasename
TO DISK = ' E:\DS_Preperation\abc.bak';

SQL ALTER TABLE Statement

You might also like