SQL Notes
SQL Notes
===
SELECT column1, column2, ...
FROM table_name;
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
===
DELETE 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;
================================================================================
STORED PROCEDURE
================================================================================
if a Table Name is "Customers"
-----------------------------
CREATE PROCEDURE SelectAllCustomoers
AS
SELECT * from Customers
GO;
How to execute:
EXEC SelectAllCustomoers;
--------------------------------------
How to execute:
EXEC SelectAllCustomers City = "London";
--------------------------------------
================================================================================
INDEX
================================================================================
================================================================================
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
--------------
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
================================================================================
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.
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?
***** 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
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 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.