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

SQL Notes

Uploaded by

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

SQL Notes

Uploaded by

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

SELECT * FROM Customers;

===
SELECT column1, column2, ...
FROM table_name;

SELECT CustomerName, City FROM Customers;


===
SELECT DISTINCT column1, column2, ...
FROM table_name;
===
SELECT COUNT(DISTINCT Country) FROM Customers;

the above statement count the distinct numbers of country


===
SELECT column1, column2, ...
FROM table_name
WHERE condition;

SELECT * FROM Customers


WHERE Country='Mexico';
===
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT * FROM Customers


WHERE Country='Germany' AND City='Berlin';
--------------
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

SELECT * FROM Customers


WHERE City='Berlin' OR City='München';
--------------
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

SELECT * FROM Customers


WHERE NOT Country='Germany';
===
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

SELECT * FROM Customers


ORDER BY Country ASC, CustomerName DESC;
===
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode,


Country)
VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
--------------
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
===
SELECT column_names
FROM table_name
WHERE column_name IS NULL;

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NULL;
--------------
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NOT NULL;
===
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
===
DELETE FROM table_name
WHERE condition;

DELETE FROM Customers


WHERE CustomerName='Alfreds Futterkiste';
===
SELECT MIN(column_name)
FROM table_name
WHERE condition;

SELECT MIN(Price) AS SmallestPrice


FROM Products;
--------------
SELECT MAX(column_name)
FROM table_name
WHERE condition;

SELECT MAX(Price) AS LargestPrice


FROM Products;
===
SELECT COUNT(column_name)
FROM table_name
WHERE condition;

SELECT COUNT(ProductID)
FROM Products;
--------------
SELECT AVG(column_name)
FROM table_name
WHERE condition;

SELECT AVG(Price)
FROM Products;
--------------
SELECT SUM(column_name)
FROM table_name
WHERE condition;

SELECT SUM(Quantity)
FROM OrderDetails;
===
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;

SELECT * FROM Customers


WHERE CustomerName LIKE 'a%';
===
CREATE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = "Brazil";

SELECT * FROM [Brazil Customers];


--------------
CREATE VIEW [Products Above Average Price] AS
SELECT ProductName, Price
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);

SELECT * FROM [Products Above Average Price];


===

================================================================================
STORED PROCEDURE
================================================================================
if a Table Name is "Customers"
-----------------------------
CREATE PROCEDURE SelectAllCustomoers
AS
SELECT * from Customers
GO;

How to execute:
EXEC SelectAllCustomoers;
--------------------------------------

If from a particular city:


CREATE PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
SELECT * FROM Customers WHERE City = @City
GO;

How to execute:
EXEC SelectAllCustomers City = "London";
--------------------------------------

For multiple paremeters


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

================================================================================
INDEX
================================================================================

CREATE INDEX index_name


ON table_name (column1, column2, ...);

CREATE UNIQUE INDEX index_name


ON table_name (column1, column2, ...);

CREATE INDEX index_name


ON table_name (column1, column2, ...);

CREATE INDEX idx_pname


ON Persons (LastName, FirstName);

DROP INDEX table_name.index_name;

================================================================================
VIEWS
================================================================================

Creating Views
--------------
CREATE VIEW [Current Product List] AS
SELECT ProductID, ProductName
FROM Products
WHERE Discontinued = No;

Using Views
-----------
SELECT * FROM [Current Product List];

Creating Views
--------------

CREATE VIEW [Products Above Average Price] AS


SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice > (SELECT AVG(UnitPrice) FROM Products);

Using Views
-----------
SELECT * FROM [Products Above Average Price];

Creating Views
--------------
CREATE VIEW [Category Sales For 1997] AS
SELECT DISTINCT CategoryName, Sum(ProductSales) AS CategorySales
FROM [Product Sales for 1997]
GROUP BY CategoryName;

Using Views
-----------
SELECT * FROM [Category Sales For 1997];
Again
-----
SELECT * FROM [Category Sales For 1997]
WHERE CategoryName = 'Beverages';

================================================================================
SQL JOIN
================================================================================

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


FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

The INNER JOIN keyword selects records that have matching values in both tables.
The LEFT JOIN keyword returns all records from the left table (table1), and the
matched records from the right table (table2).
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1).
The FULL OUTER JOIN keyword return all records when there is a match in either left
(table1) or right (table2) table records.
A self JOIN is a regular join, but the table is joined with itself.

================================================================================

================================================================================
The UNION operator is used to combine the result-set of two or more SELECT
statements.

SELECT City FROM Customers


UNION
SELECT City FROM Suppliers
ORDER BY City;

The above code only selects distinct cities.


--------------------------------------------
Again
SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers
ORDER BY City;

The aboce code select all cities


--------------------------------------------
Again
SELECT City, Country FROM Customers
WHERE Country='Germany'
UNION
SELECT City, Country FROM Suppliers
WHERE Country='Germany'
ORDER BY City;

The above SQL statement selects all the different German cities (only distinct
values) from "Customers" and "Suppliers":
--------------------------------------------

Important Questions:
===================
Difference between Stored Procedure and Function?
Difference between use of "Where" and "Having" in terms of while using grouping
How do we use order by or group by in colsole application
Difference between EXISTS and IN in SQL?
difference between like and wildcard in sql?

***What's the difference between “LIKE” and “=” in SQL?


LIKE allows partial matching / use of wildcards, while = checks for exact matches.

***** http://www.sqlcourse.com/intro.html

https://www.google.com/search?ei=4gsDXLHvFs-
AyAOhxoqoAw&q=how+to+count+data+in+one+table+and+update+into+another&oq=how+to+coun
t+data+in+one+table+and+update+&gs_l=psy-
ab.3.0.33i22i29i30.366706.400558..402035...24.0..0.546.7503.36j4j4j5j2j1......0....
1..gws-wiz.....6..0j0i71j35i39j0i67j0i131j0i10j0i20i263j0i22i30.sGB0vAFigts
*** https://stackoverflow.com/questions/1216175/mysql-count-records-from-one-table-
and-then-update-another

*** UX/UI Design Patterns


Stored Procedure VS Function
Stored Procedure vs VIEWS
Stored Procedure vs Trigger
Stored Procedure vs Query

Difference between Stored Procedure and Function:


=================================================
Function must return a value but in Stored Procedure it is optional( Procedure can
return zero or n values). Functions can have only input
parameters for it whereas Procedures can have input/output parameters . ...
Functions can be called from Procedure whereas Procedures cannot
be called from Function

Stored Procedures are pre-compiled objects which are compiled for the first time
and its compiled format is saved, which executes (compiled code)
whenever it is called.

A function is compiled and executed every time whenever it is called. A function


must returns a value and cannot modify the data received as parameters.

Difference between Stored Procedure and Views:


==============================================
A view is faster as it displays data from the tables referenced whereas a store
procedure executes sql statements. Main difference is that when
you are querying a view then it's definition is pasted into your query. ... A
Stored Procedure is like a Function, but is called Directly by
its name.

A view cannot accept parameters and also is limited to just one SELECT query as
well as not being able to perform modifications to any table.

SELECT COUNT(CustomerID) AS Ras, Country


FROM Customers
GROUP BY Country
HAVING Ras>9
ORDER BY COUNT(CustomerID) ASC;

You might also like