sql-notes
sql-notes
SQL Notes
For example:
CREATE TABLE Employee
(Employee Id INTEGER PRIMARY KEY not null,
First name VARCHAR (50),
Last name VARCHAR (75);
The mandatory semi-colon at the end of the statement is used to process every command before it. In this
example, the string CHAR is used to specify the data type.
SELECT command
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
SELECT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE Condition;
Here, column1, column2, ... are the field names of the table you want to select data from. If you want to
select all the fields available in the table, use the following syntax:
SELECT *
FROM table_name
WHERE Condition;
Example:
SELECT StudentID, StudentFName, Age
FROM Student
WHERE StudentFName=’John’;
INSERT command
The INSERT query command in SQL provides a way to add new rows of information or data inside a
specific database of the RDBMS. INSERT can be executed using two syntaxes:
Syntax:
INSERT INTO TABLE_NAME (column1, column2, column3,…columnN)
VALUES (value1, value2, value3,…valueN);
Here ‘column’ represents the table column’s specific names for inserting data in the desired way.
You may avoid the column name and add the values as defined in the column previously.
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,…valueN);
Example:
Here is an example of adding three records in the customer database table:
And using the second syntax, you can add the record as that too:
INSERT INTO CUSTOMERSVALUES (6, ‘Shubhra’, 45, ‘MP’, 4500.00 );
UPDATE Command
The UPDATE command in SQL is used to modify or change the existing records in a table. If we want to
update a particular value, then we use the WHERE clause along with the UPDATE clause.
Syntax
UPDATE table_name
SET col1=val1, col2=val2…
[Where condition];
Where
UPDATE, SET, and WHERE are the keywords
table_name is the name of the table you want update
col1, col2, … are the columns considered to be updated
val1, val2, … assign new values
the condition section is where the condition is given, followed by a semicolon.
DELETE Command
The DELETE statement is used to delete existing records in a table.
DELETE Syntax
DELETE FROM table_name
WHERE condition;
Example
The following SQL statement deletes the customer "Alfreds Futterkiste" from the "Customers" table.
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';
AVERAGE FUNCTION
The AVG() function returns the average value of a numeric column.
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM FUNCTION
The SUM() function returns the total sum of a numeric column.
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
EXAMPLES
Below is a selection from the "Products" table in the Northwind sample atabase:
2 Chang 1 1 24 - 12 oz bottles 19
Count Example
The following SQL statement finds the number of products
SELECT COUNT(ProductID)AS ProductCount
FROM Products;
Average Example
The following SQL statement finds the average price of all products:
SELECT AVG(Price)
FROM Products;
6
MIN() Example
The following SQL statement finds the price of the cheapest product:
SELECT MIN(Price) AS SmallestPrice
FROM Products;
MAX() Example
The following SQL statement finds the price of the most expensive product:
SUM Example
Below is a selection from the "Order Details" table in the Northwind sample database:
1 10248 11 12
2 10248 42 10
3 10248 72 5
4 10249 14 9
5 10249 51 40
The following SQL statement finds the sum of the "Quantity" fields in the "Order Details" table
SELECT SUM(Quantity)
FROM OrderDetails;
Below is a selection from the "Customers" table in the Northwind sample database:
UPDATE Table
The following SQL statement updates the first customer (CustomerID = 1) with a new contact person and a
new city.
Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
8
ORDERS TABLE
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
CUSTOMERS TABLE
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:
Example
SELECT Orders.OrderID, CustomerName, OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;
9
Resulting table
10