Basic Commands in SQL Server
Basic Commands in SQL Server
Table Store_Information
Result:
store_name
Los Angeles
San Diego
Los Angeles
Boston
we key in,
Result:
store_name
Los Angeles
San Diego
Boston
Result:
store_name
Los Angeles
Table Store_Information
SELECT store_name FROM Store_Information WHERE Sales > 1000 OR (Sales < 500 AND Sales > 275)
Result:
store_name
Los Angeles
San Francisco
Table Store_Information
we key in,
Result:
Table Store_Information
we key in,
Note that date may be stored in different formats in different databases. This tutorial simply choose one of the
formats.
Result:
Result:
we key in,
Result:
Result:
AVG(Sales)
$687.5
Result:
Count(store_name)
4
Result:
Count(DISTINCT store_name)
3
Result:
MAX(Sales)
$1500
Result:
MIN(Sales)
$250
Result:
SUM(Sales)
$2750
Table Store_Information
Result:
store_name SUM(Sales)
Los Angeles $1800
San Diego $250
Boston> $700
SELECT store_name, SUM(sales) FROM Store_Information GROUP BY store_name HAVING SUM(sales) >
1500
Result:
store_name SUM(Sales)
Los Angeles $1800
Result:
Result:
Table Store_Information
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
SELECT A1.region_name REGION, SUM(A2.Sales) SALES FROM Geography A1, Store_Information A2
WHERE A1.store_name = A2.store_name GROUP BY A1.region_name
Result:
REGION SALES
East $700
West $2050
Table Store_Information
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
Note that in this case, we are using the Oracle syntax for outer join.
Result:
store_name SALES
Boston $700
New York
Los Angeles $1800
San Diego $250
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
SQL Server:
SELECT region_name + ' ' + store_name FROM Geography WHERE store_name = 'Boston';
Result:
'East Boston'
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
Example 1:
Result:
's Angeles'
Example 2:
Result:
'an D'
Example 1:
Result:
'Sample'
Example 2:
Result:
'Sample '
Example 3:
Result:
' Sample'
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
Example 1:
Result:
11
Example 2:
Result:
region_name Length(region_name)
East 4
East 4
West 4
West 4
Result:
region_name
Eastern
Eastern
West
West
Table Store_Information
Table Internet_Sales
Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750
and we want to find out all the dates where there is a sales transaction. To do so, we use the following SQL statement:
Result:
Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999
Table Store_Information
Table Internet_Sales
Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750
and we want to find out all the dates where there is a sales transaction at a store as well as all the dates where there is a
sale over the internet. To do so, we use the following SQL statement:
SELECT Date FROM Store_Information UNION ALL SELECT Date FROM Internet_Sales
Result:
Date
Jan-05-1999
Jan-07-1999
Jan-08-1999
Jan-08-1999
Jan-07-1999
Jan-10-1999
Jan-11-1999
Jan-12-1999
Table Store_Information
Table Internet_Sales
Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750
and we want to find out all the dates where there are both store sales and internet sales. To do so, we use the following SQL
statement:
Result:
Date
Jan-07-1999
Table Store_Information
Table Internet_Sales
Date Sales
Jan-07-1999 $250
Jan-10-1999 $535
Jan-11-1999 $320
Jan-12-1999 $750
and we want to find out all the dates where there are store sales, but no internet sales. To do so, we use the following SQL
statement:
Result:
Date
Jan-05-1999
Jan-08-1999
Table Store_Information
we key in,
SELECT store_name, Sales, Date FROM Store_Information ORDER BY Sales DESC LIMIT 2;
Result:
Table Store_Information
SELECT TOP 2 store_name, Sales, Date FROM Store_Information ORDER BY Sales DESC;
Result:
Alternatively, if we want to show the top 25% of sales amounts from Table Store_Information, we key in,
SELECT TOP 25 PERCENT store_name, Sales, Date FROM Store_Information ORDER BY Sales DESC;
Result:
Table Store_Information
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
and we want to use a subquery to find the sales of all stores in the West region. To do so, we use the following SQL
statement:
SELECT SUM(Sales) FROM Store_Information WHERE Store_name IN (SELECT store_name FROM Geography
WHERE region_name = 'West')
Result:
SUM(Sales)
2050
SELECT SUM(Sales) FROM Store_Information WHERE EXISTS SELECT * FROM Geography WHERE
region_name = 'West')
2750
if we want to multiply the sales amount from 'Los Angeles' by 2 and the sales amount from 'San Diego' by 1.5, we key in,
SELECT store_name, CASE store_name WHEN 'Los Angeles' THEN Sales * 2 WHEN 'San Diego' THEN Sales * 1.5
ELSE Sales END "New Sales", Date FROM Store_Information
"New Sales" is the name given to the column with the CASE statement.
Result:
In SQL Server, the ISNULL() function is used to replace NULL value with another value.
Table Sales_Data
store_name Sales
Store A 300
Store B NULL
returns 400. This is because NULL has been replaced by 100 via the ISNULL function.
MySQL
In MySQL, the ISNULL() function is used to test whether an expression is NULL. If the expression is NULL, this function
returns 1. Otherwise, this function returns 0.
For example,
ISNULL(3*3) returns 0
ISNULL(3/0) returns 1
Table Sales_Data
store_name Sales
Store A 300
Store B NULL
Store C 150
returns 550. This is because NULL has been replaced by 100 via the ISNULL function, hence the sum of the 3 rows is 300 +
100 + 150 = 550.
Table Contact_Info
and we want to find out the best way to contact each person according to the following rules:
2. If a person does not have a business phone and has a cell phone, use the cell phone number.
3. If a person does not have a business phone, does not have a cell phone, and has a home phone, use the home phone
number.
Result:
Name Contact_Phone
Jeff 531-2531
Laura 772-5588
Peter 594-7477
Table Sales_Data
Store_name NULLIF(Actual,Goal)
Store A NULL
Store B 40
Store C 25
Table Total_Sales
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20
we would type,
Result:
Table Total_Sales
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20
we would type,
SELECT Sales Median FROM (SELECT a1.Name, a1.Sales, COUNT(a1.Sales) Rank FROM Total_Sales a1,
Total_Sales a2 WHERE a1.Sales < a2.Sales OR (a1.Sales=a2.Sales AND a1.Name <= a2.Name) group by a1.Name,
a1.Sales order by a1.Sales desc) a3 WHERE Rank = (SELECT (COUNT(*)+1) DIV 2 FROM Total_Sales);
Result:
Median
20
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20
we would type,
Result:
Table Total_Sales
Name Sales
John 10
Jennifer 15
Stella 20
Sophia 40
Greg 50
Jeff 20
we would type,
SELECT a1.Name, a1.Sales, SUM(a2.Sales)/(SELECT SUM(Sales) FROM Total_Sales) Pct_To_Total
FROM Total_Sales a1, Total_Sales a2 WHERE a1.Sales <= a2.sales or (a1.Sales=a2.Sales and a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales ORDER BY a1.Sales DESC, a1.Name DESC;
Result:
will result in an error because '3' already exists in the SID column, thus trying to insert another row with that value violates the
UNIQUE constraint.
Please note that a column that is specified as a primary key must also be unique. At the same time, a column that's unique
may or may not be a primary key. In addition, multiple UNIQUE constraints can be defined on a table.
Please note that the CHECK constraint does not get enforced by MySQL at this time
SQL Server:
CREATE TABLE Customer (SID integer PRIMARY KEY, Last_Name varchar(30), First_Name varchar(30));
SQL Server:
ALTER TABLE Customer ADD PRIMARY KEY (SID);
Note: Before using the ALTER TABLE command to add a primary key, you'll need to make sure that the field is defined as
'NOT NULL' -- in other words, NULL cannot be an accepted value for that field
Table CUSTOMER
Table ORDERS
In the above example, the Customer_SID column in the ORDERS table is a foreign key pointing to the SID column in the
CUSTOMER table.
Below we show examples of how to specify the foreign key when creating the ORDERS table:
SQL Server:
CREATE TABLE ORDERS (Order_ID integer primary key, Order_Date datetime, Customer_SID integer references
CUSTOMER(SID), Amount double);
Below are examples for specifying a foreign key by altering a table. This assumes that the ORDERS table has been created,
and the foreign key has not yet been put in:
SQL Server:
ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);
SQL VIEW
1. Ease of use: A view hides the complexity of the database tables from end users. Essentially we can think of views as a
layer of abstraction on top of the database tables.
2. Space savings: Views takes very little space to store, since they do not store actual data.
3. Additional data security: Views can include only certain columns in the table so that only the non-sensitive columns are
included and exposed to the end user. In addition, some databases allow views to have different security settings, thus hiding
sensitive data from prying eyes.
Table Store_Information
Table Geography
region_name store_name
East Boston
East New York
West Los Angeles
West San Diego
and we want to build a view that has sales by region information. We would issue the following SQL statement:
This gives us a view, V_REGION_SALES, that has been defined to store sales by region records. If we want to find out the
content of this view, we type in,
Result:
REGION SALES
East $700
West $2050
1. Build index on columns of integer type
Integers take less space to store, which means the query will be faster. If the column you want to build an index for is not of
type integer, consider creating a surrogate integer key (or simply a surrogate column of type integer) which maps one-to-one
to the column you want to build the index for.
Narrower indexes take less space, require less time to process, which in turn means the query will run faster.
For indexes covering multiple columns, the order of the columns in the index is important. The best practice is to use the
column with the lowest cardinality first, and the column with the highest cardinality last. Recall cardinality means the number
of distinct values for that column. So, if "SELECT DISTINCT (COLUMN1) FROM TABLE_NAME;" returns 5, that means the
cardinality for COLUMN1 is 5.
4. Make sure the column you are building an index for is declared NOT NULL
This can decrease the size of the index, which in turn will speed up the query.
If we want to create an index on both City and Country, we would type in,
There is no strict rule on how to name an index. The generally accepted method is to place a prefix, such as "IDX_", before
an index name to avoid confusion with other database objects. It is also a good idea to provide information on which table
and column(s) the index is used on.
Please note that the exact syntax for CREATE INDEX may be different for different databases. You should consult with your
database reference manual for the precise syntax.
Table customer
Table customer
Table customer
Then, we want to change the data type for "Addr" to char(30). To do this, we key in,
Table customer
Table customer
So, if we wanted to drop the table called customer that we created in the CREATE TABLE section, we simply type
So, if we wanted to truncate the table called customer that we created in SQL CREATE TABLE, we simply type,
USE Scores;
In MySQL, you can access tables in multiple databases by specifying [Database Name].[Table Name]. If the table you want
to access is currently in the database you use, there is no need to specify the database name.
For example, if you want to access table "Course_110" from database "Scores" and table "Students" from database
"Personnel", you can type in the following:
USE Scores;
SELECT ...
FROM Course_110, Personnel.Students
WHERE ...
;
INSERT INTO Store_Information (store_name, Sales, Date) SELECT store_name, Sales, Date FROM
Sales_Information WHERE Year(Date) = 1998
Table Store_Information
and we notice that the sales for Los Angeles on 01/08/1999 is actually $500 instead of $300, and that particular entry needs
to be updated. To do so, we use the following SQL:
UPDATE Store_Information SET Sales = 500 WHERE store_name = "Los Angeles" AND Date = "Jan-08-1999"
Table Store_Information
Table Store_Information
and we decide not to keep any information on Los Angeles in this table. To accomplish this, we type the following SQL:
Table Store_Information
=================================
============