Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language

Similar presentations


Presentation on theme: "Structured Query Language"— Presentation transcript:

1 Structured Query Language
56:150 Information System Design

2 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”.

3 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]

4 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

5 Use alias for tables SELECT prdct.productname], ctg.categoryname
FROM products as prdct, categories as ctg WHERE ctg.CategoryID = prdct.categoryID;

6 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’)

7 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.

8 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

9 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

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

11 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

12 Update Syntax UPDATE table_name
SET column_name = expression [WHERE search _ conditions] Without where clause, update operation will affect all rows.

13 Delete Syntax DELETE FROM table_name
[WHERE search contiditions] Without where clause, delete operation will influence all the rows.

14 Insert Syntax INSERT INTO tablename [(columnname, …)] VALUES (constant, …)

15 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

16 Other topics Transactions Stored-Procedures Triggers Outer Join Union
Data administration


Download ppt "Structured Query Language"

Similar presentations


Ads by Google