Structured Query Language 56:150 Information System Design
Introduction A unified language for defining, querying, modifying, and controlling the data in a relational database. (data manipulation, data definition, and data administration) Officially pronounced as “ess-cue-ell”. But many people say “sequel”.
Select It lets you find and view your data in a variety of ways. Basic Syntax SELECT list_of_columns FROM tables[s] [WHERE search_conditions]
Examples SELECT * FROM products (* means all columns) SELECT ProductName, Unitprice * UnitsOnOrder as [Ordered Amount] FROM products WHERE UnitsOnOrder > 0 SELECT ‘The highest price is ’, max(unitprice) FROM Products
Use alias for tables SELECT prdct.productname], ctg.categoryname FROM products as prdct, categories as ctg WHERE ctg.CategoryID = prdct.categoryID;
Where Clause Comparison operators (=, < , >, < >, and so on) Combinations or logical negations of conditions (AND, OR, NOT) Where unitprice < 5000 and unitprice > 2000 Ranges (Between, Not Between) Where unitprice between 2000 and 5000 Lists (In, Not in) Where state in (‘CA’, ‘IN’, ‘MD’)
Where Clause Unknown Values (Is Null, Is not Null) Where productname is null Character matches (Like and Not like) Where phone not like ‘415%’ Wildcard % , any string of zero or more characters Wildcard _ , any single character.
Other selection techniques Order by {expression [ASC | DESC]} Select productname, unitprice from products where unitprice between 20 and 50 order by price Distinct select distinct categoryID from products Aggregate functions sum, avg, count, max, min
Other selection techniques Group by: divides the rows into sets Having: puts a condition on the sets Where eliminates rows before grouping, but having goes to work after grouping. Example: SELECT categoryID, avg(unitprice), count(productname) FROM products group by categoryID having count(productname) > 10
Query with subquery A subquery is a SELECT statement that nests inside the WHERE, HAVING, or SELECT clause of another SELECT statement; Inside an INSERT, UPDATE, or DELETE statement; Inside another subquery
Example SELECT CustomerID FROM orders WHERE orderid in (select orderid from [order details] where discount=0.15) Alternative Way? Select o.customerid from orders as o, [order details] as od Where o.orderid = od.orderid and od.discount = 0.15
Update Syntax UPDATE table_name SET column_name = expression [WHERE search _ conditions] Without where clause, update operation will affect all rows.
Delete Syntax DELETE FROM table_name [WHERE search contiditions] Without where clause, delete operation will influence all the rows.
Insert Syntax INSERT INTO tablename [(columnname, …)] VALUES (constant, …)
Create Create Tables Syntax Create Index Syntax CREATE TABLE table_name (column_name datatype [NULL | NOT NULL] [, column_name datatype [NULL | NOT NULL] ]…) Create Index Syntax CREATE [UNIQUE] INDEX index_name ON table_name (column_name) Database, view can also be created by SQL
Other topics Transactions Stored-Procedures Triggers Outer Join Union Data administration