Summary of SQL Commands
Summary of SQL Commands
Summary of SQL Commands
html
SQL Commands
Database
A database consists of one or more tables. A table is identified by its name. A table is made up of columns and rows. Columns contain the column
name and data type. Rows contain the records or data for the columns.
Basic SQL
Each record has a unique identifier or primary key. SQL, which stands for Structured Query Language, is used to communicate with a database.
Through SQL one can create and delete tables. Here are some commands:
SELECT
The SELECT is used to query the database and retrieve selected data that match the specific criteria that you specify:
= Equal
> Greater than
1 of 7 4/22/2014 10:51 PM
Summary of SQL Commands http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
Use single quotes around text values (most database systems will also accept double quotes). Numerical values should not be enclosed in quotes.
CREATE TABLE
The CREATE TABLE statement is used to create a new table. The format is:
INSERT VALUES
Once a table has been created data can be inserted using INSERT INTO command.
2 of 7 4/22/2014 10:51 PM
Summary of SQL Commands http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
UPDATE
To change the data values in a pre existing table, the UPDATE command can be used.
UPDATE tablename
SET colX = valX [, colY = valY, ...]
WHERE condition
DELETE
To delete all the records from a table without deleting the table do
DROP
To remove an entire table from the database use the DROP command.
ORDER BY
ORDER BY clause can order column name in either ascending (ASC) or descending (DESC) order.
AND / OR
AND and OR can join two or more conditions in a WHERE clause. AND will return data when all the conditions are true. OR will return data
3 of 7 4/22/2014 10:51 PM
Summary of SQL Commands http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
IN
IN operator is used when you know the exact value you want to return for at least one of the columns
BETWEEN / AND
The BETWEEN ... AND operator selects a range of data between two values. These values can be numbers, text, or dates.
JOIN
There are times when we need to collate data from two or more tables. That is called a join. Tables in a database are related to each other through
their keys. We can associate data in various tables without repeating them. For example we could have a table called Customers which could have
information about customers like their name, address, phone numbers. We could have another table called Products that has information regarding
the products like part number, product name, manufacturer, number in stock, unit price. A third table called Orders could have information
regarding what product was ordered, by whom, the date the order was placed, and quantity. Here are the tables:
Customers
Cust_ID FirstName LastName Address Phone
01 Mickey Mouse 123 Gouda St. 456-7890
02 Donald Duck 325 Eider Ln. 786-2365
Products
Part_No Name Manufacturer In_Stock Price
20-45 Hammer Stanley 57 3.50
21-68 ScrewDriver DeVries 84 2.75
Orders
Order_No Part_No Cust_ID Date Quantity
2005-27 21-68 02 31 Oct 2005 2
4 of 7 4/22/2014 10:51 PM
Summary of SQL Commands http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
We can select data from two tables with INNER JOIN. The INNER JOIN returns all rows from both tables where there is a match. If there are
rows in Customers that do not have matches in Orders, those rows will not be listed.
SELECT Customers.FirstName, Customers.LastName, Orders.Date
FROM Customers
INNER JOIN Orders
ON Customers.Cust_ID = Orders.Cust_ID
The LEFT JOIN returns all the rows from the first table (Customers), even if there are no matches in the second table (Orders). If there are rows in
Customers that do not have matches in Orders, those rows also will be listed.
SELECT Customers.FirstName, Customers.LastName, Orders.Date
FROM Customers
LEFT JOIN Orders
ON Customers.Cust_ID = Orders.Cust_ID
The RIGHT JOIN returns all the rows from the second table (Orders), even if there are no matches in the first table (Customers). If there had been
any rows in Orders that did not have matches Customers, those rows also would have been listed.
SELECT Customers.FirstName, Customers.LastName, Orders.Date
FROM Customers
RIGHT JOIN Orders
ON Customers.Cust_ID = Orders.Cust_ID
ALTER TABLE
With ALTER TABLE you can add or delete columns in an existing table. When you add a column you must specify a data type.
ALTER TABLE table_name
ADD col_name datatype
5 of 7 4/22/2014 10:51 PM
Summary of SQL Commands http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
UNION
The UNION command is used to select data from two tables very similar to the JOIN command. But the UNION command can be used only with
columns having the same datatype. With UNION only distinct values are selected, i.e. if there are common data in the two tables only one instance
of that data is returned.
This will select all the customers from USA and Asia but if there is a name that occurs in both the tables it will return only one such name. To get
all the names use UNION ALL instead.
SQL Functions
There are several built-in functins in SQL. The basic function types are:
Aggregate Functions: These are functions that operate against a collection of values, but return a single value.
Scalar Functions: These functions operate against a single value, and return a single value.
GROUP BY
The GROUP BY was added to SQL so that aggregate functions could return a result grouped by column values.
HAVING keyword was introduced because the WHERE keyword could not be used. HAVING states a condition.
CREATE VIEW
6 of 7 4/22/2014 10:51 PM
Summary of SQL Commands http://www.cs.utexas.edu/~mitra/csFall2013/cs329/lectures/sql.html
A view is a virtual table that is a result of SQL SELECT statement. A view contains fields from one or more real tables in the database. This virtual
table can then be queried as if it were a real table.
A view could be used from inside a query, a stored procedure, or from inside another view. You can add functions and joins to a view and present
the data you want to the user.
7 of 7 4/22/2014 10:51 PM