Professional Documents
Culture Documents
The SQL SELECT statement is the SQL command that retrieves data from an SQL database. This operation is also known as a query and is the key to the use of SQL for analysis and reporting. This tutorial section shows the basic SQL SELECT statement so see the SQL Advanced tutorial section for more in depth knowledge.
SQL SELECT Statement Example 1 The following example retrieves the columns product_nbr and product_name from all rows of the product table.
Results from the execution of the SQL SELECT statement, also known as a query are as follows: product_nbr product_name 1001 SQL Tool 1.0 2001 SQL Tool 2.0 Light 2002 SQL Tool 2.0 Professional 2003 SQL Tool 2.0 Enterprise SQL SELECT Statement Example 2 The following example retrieves all of the columns from all rows of the product table.
Results show the product_status_code column that was not present in Example 1. product_nbr product_name product_status_code 1001 SQL Tool 1.0 Inactive 2001 SQL Tool 2.0 Light Active 2002 SQL Tool 2.0 Professional Active 2003 SQL Tool 2.0 Enterprise Active
SQL CREATE DATABASE Statement Example The following example creates a new database named CUSTOMER_DB.
Correct use of indexes can make the difference between a top performing database with high customer satisfaction and a non-performing database with low customer satisfaction.
CREATE UNIQUE INDEX <index_type> <index_name> ON <table_name> ( <column_name1> <index_order>, <column_name2> <index_order>, )
The number of characters that can make up SQL names for tables, columns and indexes varies by DBMS. In many cases the limit is 30 characters. The leading character of the name must be alphabetic not a number or special character. The name of a new index can not duplicate the name of an existing index for the same table and should not be the same as a SQL reserved word. The underscore character can be used to improve readability. List elements are seperated by commas. SQL CREATE INDEX Statement Example 1 - Rapid Access The following example creates an non-unique index named IDX_PERSON_NAME on table PERSON with columns named person_name and gender_code in ascending sequence.. Here are the contents of the table: Column Name person_id person_name Datatype INT Nullability NOT NULL
VARCHAR(50) NULL VARCHAR(50) NOT NULL CHAR(2) CHAR(9) NOT NULL NOT NULL
SQL CREATE INDEX Statement Example 2 - Enforce Uniqueness The following example creates a unique index named IDX_PERSON_SSN on table PERSON with the column named social_security_nnbr. This assures that two rows can not be added to the PERSON table with the same social security number. This SQL CREATE TABLE Statement is executed:
CREATE TABLE <table_name> ( <column_name1> <datatype1> <constraint1> <column_name2> <datatype2> <constraint2> <constraint-list> ) The number of characters that can make up SQL table names and column names varies by DBMS. In many cases the limit is 30 characters. The leading character of the name must be alphabetic - not a
number or special character. The name of a new table can not duplicate the name of an existing table and should not be the same as a SQL reserved word. The underscore character can be used to improve readability. The same column name can not be repeated within a table. List elements are seperated by commas. Here are some example datatypes: SQL Datatype Description integer(size) int(size) Integers smallint(size) tinyint(size) decimal(size,decimals) Numbers with decimals numeric(size,decimals) char(size) Fixed length character string varchar(size) Variable length character string A date in yyyymmdd format date SQL CREATE TABLE Statement Example The following example creates a table named product with columns named product_nbr, product_name, product_status_code, start_date, end_date andraw_material_c ost_amt. This SQL CREATE TABLE Statement is executed:
CREATE TABLE product ( product_nbr int NOT NULL, product_name varchar(40) NOT NULL, product_status_code char(10) NOT NULL, start_date date NULL, end_date date NULL, raw_material_cost decimal(12,2) NULL, primary key (product_nbr) )
Database queries are simplified Database complexity is hidden Flexibility is increased - queries of views may not change when underlying tables chagne Security is increased - sensitive information can be excluded from a view
The number of characters that can make up SQL names for tables, columns and views varies by DBMS. In many cases the limit is 30 characters. The leading character of the name must be alphabetic - not a number or special character. The name of a new view can not duplicate the name of an existing view or table and should not be the same as a SQL reserved word. The underscore character can be used to improve readability. List elements are seperated by commas. SQL CREATE VIEW Statement Example - Data Filtering The following example creates a view named V_TOP_CUSTOMER on table CUSTOMER with the following columns: customer_id customer_name ytd_sales_amt zip_code
Here are the contents of the table: Column Name Datatype Nullability customer_id INT NOT NULL customer_name VARCHAR(20) NOT NULL ytd_sales_amt MONEY NOT NULL line_1_addr VARCHAR(50) NULL city_name VARCHAR(50) NOT NULL state_code CHAR(2) NOT NULL zip_code CHAR(9) NOT NULL This SQL CREATE VIEW Statement is executed:
customer_name, ytd_sales_amt, zip_code) AS SELECT customer_id, customer_name, ytd_sales_amt, zip_code FROM CUSTOMER WHERE ytd_sales_amt > 1200.00
SQL ALTER TABLE Statement Example 1 - Add a Column The following example adds a new column into the person table. Before the operation takes place the following columns exists in the table: Column Name Datatype Nullability person_id INT NOT NULL person_name VARCHAR(20) NOT NULL
CHAR(1) NULL VARCHAR(50) NULL VARCHAR(50) NULL VARCHAR(50) NULL VARCHAR(50) NOT NULL CHAR(2) NOT NULL CHAR(9) NOT NULL
Results from the execution of the SQL ALTER TABLE statement are: Column Name Datatype Nullability person_id INT NOT NULL person_name VARCHAR(20) NOT NULL gender_code CHAR(1) NULL line_1_addr VARCHAR(50) NULL line_2_addr VARCHAR(50) NULL apartment_nbr VARCHAR(50) NULL city_name VARCHAR(50) NOT NULL state_code CHAR(2) NOT NULL zip_code CHAR(9) NOT NULL marital_status_code CHAR(1) NULL SQL ALTER TABLE Statement Example 2 - Alter a Column Datatype The following example changes the datatype of an existing column in the person table. Before the operation takes place the following columns exists in the table: Column Name Datatype Nullability person_id INT NOT NULL person_name VARCHAR(20) NOT NULL gender_code CHAR(1) NULL line_1_addr VARCHAR(50) NULL line_2_addr VARCHAR(50) NULL apartment_nbr VARCHAR(50) NULL city_name VARCHAR(50) NOT NULL state_code CHAR(2) NOT NULL zip_code CHAR(9) NOT NULL This ALTER TABLE Statement is executed:
Results from the execution of the SQL ALTER TABLE statement are: Column Name Datatype Nullability person_id INT NOT NULL VARCHAR(50) NOT NULL person_name gender_code CHAR(1) NULL line_1_addr VARCHAR(50) NULL line_2_addr VARCHAR(50) NULL apartment_nbr VARCHAR(50) NULL city_name VARCHAR(50) NOT NULL state_code CHAR(2) NOT NULL zip_code CHAR(9) NOT NULL marital_status_code CHAR(1) NULL SQL ALTER TABLE Statement Example 3 - Drop a column The following example removes an existing column in the person table. Before the operation takes place the following columns exists in the table: Column Name Datatype Nullability person_id INT NOT NULL person_name VARCHAR(50) NOT NULL gender_code CHAR(1) NULL line_1_addr VARCHAR(50) NULL line_2_addr VARCHAR(50) NULL apartment_nbr VARCHAR(50) NULL city_name VARCHAR(50) NOT NULL state_code CHAR(2) NOT NULL zip_code CHAR(9) NOT NULL This ALTER TABLE Statement is executed:
Results from the execution of the SQL ALTER TABLE statement are: Column Name Datatype Nullability person_id INT NOT NULL person_name VARCHAR(50) NOT NULL gender_code CHAR(1) NULL line_1_addr VARCHAR(50) NULL line_2_addr VARCHAR(50) NULL city_name VARCHAR(50) NOT NULL state_code CHAR(2) NOT NULL zip_code CHAR(9) NOT NULL
SQL JOIN Example The following example JOINs the region and branch tables on the region_nbr column. Here are the contents of the tables: Table: REGION region_nbr region_name 100 East Region 200 Central Region 300 Virtual Region 400 West Region Table: BRANCH
branch_nbr branch_name region_nbr employee_count 108 New York 100 10 110 Boston 100 6 212 Chicago 200 5 404 San Diego 400 6 415 San Jose 400 3 This SQL Statement with JOIN is executed:
SELECT region.region_nbr, region.region_name, branch.branch_nbr, branch.branch_name FROM dbo.region JOIN dbo.branch ON branch.region_nbr = region.region_nbr ORDER BY region.region_nbr
Here is the result. Note that the "Virtual Region" is included in the results even though it has no rows in the branch table. This is the difference between the INNER JOIN and OUTER JOIN. region_nbr region_name branch_nbr branch_name 100 East Region 108 New York 100 East Region 110 Boston 200 Central Region 212 Chicago 400 West Region 404 San Diego 400 West Region 415 San Jose
Important! A "cartesian product" can result if there is no relating the tables for the join. A row would be included for each combination between the two tables so if one table has 1,000 rows and the second table has 2,000 rows then 2,000,000 rows would be returned. Important! If there are matches on the JOIN criteria then rows will still be returned. This is known an "OUTER JOIN". Use the "INNER JOIN" in cases where no rows should be returned when one side of the join is missing. SQL OUTER JOIN Syntax
SELECT <column_name1>, <column_name2> <aggregate_function> FROM <table_name> LEFT OUTER JOIN <table_name> ON <join_conditions>
SQL OUTER JOIN Example The following example JOINs the region and branch tables on the region_nbr column. Here are the contents of the tables: Table: REGION region_nbr region_name 100 East Region 200 Central Region 300 Virtual Region 400 West Region Table: BRANCH branch_nbr branch_name region_nbr employee_count 108 New York 100 10 110 Boston 100 6 212 Chicago 200 5 404 San Diego 400 6 415 San Jose 400 3 This SQL Statement with OUTER JOIN is executed:
SELECT region.region_nbr, region.region_name, branch.branch_nbr, branch.branch_name FROM dbo.region LEFT OUTER JOIN dbo.branch ON branch.region_nbr = region.region_nbr ORDER BY region.region_nbr
Here is the result. Note that the "Virtual Region" is included in the results even though it has no rows in the branch table. This is the difference between the INNER JOIN and OUTER JOIN.
region_nbr region_name branch_nbr branch_name 100 East Region 108 New York 100 East Region 110 Boston 200 Central Region 212 Chicago 300 Virtual Region NULL NULL 400 West Region 404 San Diego 400 West Region 415 San Jose