Sunteți pe pagina 1din 12

SQL Commands

SQL Commands
SQL SELECT
SQL INSERT INTO
SQL WHERE
SQL DISTINCT
SQL UPDATE
SQL DELETE
SQL TRUNCATE TABLE
SQL ORDER BY
SQL Aggregate Functions
SQL GROUP BY
SQL HAVING
SQL AND & OR
SQL JOIN
SQL UNION
SQL Replication
ETL
SQL Training
SQL Hosting
© Art Branch Inc.

SQL Commands > SQL JOIN

Q1 The SQL JOIN clause selects data from two or more tables tied together by matching table
columns. To illustrate how to use the SQL JOIN clause we will use the already familiar Weather table and
we will introduce a new one called State.

Weather table

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005

State table
State City
District of Columbia Washington
Washington Seattle
New York New York
Texas Houston

If we want to select all the data from the Weather table, and to add one more additional column called State
to the result, we can do it the following way:

SELECT Weather.City, Weather.AverageTemperature, Weather.Date, State.State


FROM Weather, State
WHERE Weather.City = State.City

In the SQL SELECT list we see 4 column names prefixed with their respective table names, so there is no
confusion which column from which table we are referencing.
The SQL FROM clause has comma separated list of the two tables we are selecting data from. Finally the
SQL WHERE clause defines that the City column from the weather table has to match the City column from
the State table.

We can re-write the above SQL statement using SQL JOIN and the result will be the same, but the
performance in general will be much better:
SELECT Weather.City, Weather.AverageTemperature, Weather.Date, State.State
FROM Weather JOIN State
ON Weather.City = State.City

The first line of this SQL JOIN statement is identical to the first line of the previous SQL expression. The
second line uses the SQL JOIN clause instead of comma, and the third line uses the ON keyword to define
the columns we are joining on.

join two or more search conditions specified in the WHERE clause. Let's use the weather table to illustrate
how to use SQL AND & SQL OR keywords:

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005

Here is how to select all rows where the city is either Washington or New York:

Q 2 And ,or statement


SELECT *
FROM Weather
WHERE City = 'Washington' OR City = 'New York'

The result of this SQL OR expression will be:

City AverageTemperature Date


New York 22 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Washington 17 C 10/09/2005

The following SQL statement using the SQL AND keyword will select all dates on which the average
temperature in Washington was greater than 18 C:

SELECT Date
FROM Weather
WHERE City = 'Washington' AND AverageTemperature > 18

Here is the result of this SQL AND expression:

Date
10/10/2005

join two or more search conditions specified in the WHERE clause. Let's use the weather table to illustrate
how to use SQL AND & SQL OR keywords:

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005

Here is how to select all rows where the city is either Washington or New York:

SELECT *
FROM Weather
WHERE City = 'Washington' OR City = 'New York'
The result of this SQL OR expression will be:

City AverageTemperature Date


New York 22 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Washington 17 C 10/09/2005

The following SQL statement using the SQL AND keyword will select all dates on which the average
temperature in Washington was greater than 18 C:

SELECT Date
FROM Weather
WHERE City = 'Washington' AND AverageTemperature > 18

Here is the result of this SQL AND expression:

Date
10/10/2005

Q3 Sql replication

SQL Replication

SQL replication is a process for sharing/distributing data between different databases and synchronizing
between those databases. You can use SQL replication to distribute data to a variety of network points like
other database servers, mobile users, etc. You can perform the replication over many different kinds of
networks and this won’t affect the end result.

In every SQL replication there are 2 main players called Publisher and Subscriber. The Publisher is the
replication end point that supplies the data and the replication Subscriber is the replication end point that
uses the data from the Publisher. Depending on the replication architecture a replication can have one or
more Publishers and of course any replication will have one or more Subscribers.
MS SQL Server offers several main replication types. The Transactional replication is usually used when
there’s need to integrate data from several different locations, offloading batch processing, and in data
warehousing scenarios.

Another replication type is the Snapshot replication. The Snapshot replication is commonly performed
when a full database refresh is appropriate or as a starting point for transactional or merge replications.

The third important SQL replication type is the Merge replication. The Merge replication is used
whenever there is a possibility for a data conflicts across distributed server applications.

Q4 Sql union

The SQL UNION clause merges the results of two or more SELECT SQL queries into one result set. When
using SQL UNION, all the SQL expressions participating in the UNION must have the same structure (they
have to have the same number of columns and same or compatible data types). You have to keep the
column order of all unionized SQL SELECT expressions uniform, otherwise you’ll get an error.

Here is an example of SQL UNION statement:

SELECT City, AverageTemperature, Date


FROM Weather

UNION

SELECT City, AverageTemperature, Date


FROM WeatherArchive

The result of the SQL UNION statement above will merge all records from the Weather and the
WeatherArchive tables and the column names in the result will remain the same as the column names in the
first SELECT expression in the UNION statement. This SQL UNION expression will also remove all
duplicates (if any).

Something important to remember when using SQL UNION is that the duplicated rows are removed from the
result set. If you want all rows, including duplicate ones to be returned, simply put the ALL keyword after the
UNION clause:

SELECT City, AverageTemperature, Date


FROM Weather

UNION ALL

SELECT City, AverageTemperature, Date


FROM WeatherArchive
Q 5 Group by clause

The SQL GROUP BY clause is used along with the SQL aggregate functions and specifies the groups
where selected rows are placed. WHEN one or more aggregate functions are presented in the SQL
SELECT column list, the SQL GROUP BY clause calculates a summary value for each group. To grasp this
concept we will illustrate the SQL GROUP BY application with example. Consider the already familiar
Weather table:

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005

Our task is to calculate the average temperature for each of the cities in the Weather table. Here is how to
accomplish that using the SQL GROUP BY clause:

SELECT City, AVG(AverageTemperature)


FROM Weather
GROUP BY City

The result of this SQL GROUP BY statement is the following:

City AverageTemperature
Washington 18.5 C
Seattle 20.5 C
New York 20 C

As you can see in the result data set, we have one row for each city. Each city in our SQL ORDER BY
statement, represents one group because the City column is specified in the GROUP BY clause.
But what is the number in the AverageTemperature column? How come we have only one number for each
city when we have multiple temperature entries for each of the cities in the Weather table? Because the
AverageTemperature column is an argument for the SQL AVG aggregate function, the single value we see
in the final result set is simply the average temperature value for its respective city:

Washington average temperature = (20 + 17)/2 = 18.5


Seattle average temperature = (21 + 20)/2 = 20.5
New York average temperature = (20 + 20)/2 = 20
When GROUP BY is used, one of the following must be true:

1. Each column which is not part of an aggregate expression in the select list is presented in the SQL
GROUP BY clause.
2. The SQL GROUP BY expression matches exactly the select list expression.

Here is an example of incorrect GROUP BY statement:

SELECT City, AVG(AverageTemperature), Date


FROM Weather
GROUP BY City

This SQL GROUP BY statement is wrong because the Date column is presented in the SQL SELECT list,
but is not in the GROUP BY clause. The correct SQL GROUP BY statement should look like this:

SELECT City, AVG(AverageTemperature), Date


FROM Weather
GROUP BY City, Date

Q 6 Sql distinct

The SQL DISTINCT clause works in conjunction with the SQL SELECT clause and selects only distinct
(unique) data from a database table(s). Here is an example of SQL DISTINCT clause:

SELECT DISTINCT Column1


FROM Table1

As you can see the DISTINCT keyword goes immediately after the SELECT clause and is then followed by
a list of one or more column names. I'll give you an example why you might need to use the DISTINCT SQL
clause. I'll use the Weather table from the SQL WHERE tutorial to demonstrate the SQL DISTINCT
application:

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005

Consider the following SQL statement utilizing SQL DISTINCT:

SELECT DISTINCT City


FROM Weather
This SQL DISTINCT expression will return a list with all cities found in the City column of the Weather table,
but it will remove the duplicates and leave only a single entry for each city:

City
New York
Seattle
Washington

You can use the SQL DISTINCT with any table column for example with the AverageTemperature:

SELECT DISTINCT AverageTemperature


FROM Weather

The result of this SQL DISTINCT will be:

AverageTemperature
22 C
21 C
20 C
18 C
17 C

You can use the SQL DISTINCT with more than one column and if you do that, the result will have all
distinct combinations of values for all columns. For example if our Weather table has the following entries:

City AverageTemperature Date


New York 22 C 10/10/2005
New York 22 C 10/09/2005
New York 20 C 10/08/2005
New York 20 C 10/07/2005
New York 18 C 10/06/2005

And we run the following SQL DISTINCT statement:

SELECT DISTINCT City, AverageTemperature


FROM Weather

The result will be:

City AverageTemperature
New York 22 C
New York 20 C
New York 18 C

Q 7 Aggregate functions

are used to sum, count, get the average, get the minimum and get the maximum values from a column or
from a sub-set of column values.

To count the rows in the Weather table we can use the SQL COUNT aggregate function:

SELECT COUNT(*)
FROM Weather

To get the average temperature for the Weather table use the AVG SQL aggregate function:

SELECT AVG(AverageTemperature)
FROM Weather

If you want to get the average temperature for a particular city you can do it this way:

SELECT AVG(AverageTemperature)
FROM Weather
WHERE City = 'New York'

To get the minimum value from a numeric table column, use the SQL MIN aggregate function:

SELECT MIN(AverageTemperature)
FROM Weather

To get the maximum value from a numeric table column, use the SQL MAX aggregate function:

SELECT MAX(AverageTemperature)
FROM Weather

Finally to sum up the values in the column use the SQL SUM aggregate function:

SELECT SUM(AverageTemperature)
FROM Weather

You can specify search criteria with the SQL WHERE clause for any of the above SQL aggregate functions.
Q 8 Insert into

The SQL INSERT INTO clause facilitates the process of inserting data into a SQL table. Here is how you
can insert a new row into the Weather table, using SQL INSERT INTO:

INSERT INTO Weather (City, AverageTemperature, Date)


VALUES ('Los Angeles', 20, '10/10/2005')

The result of the execution of the SQL INSERT INTO above will look like this:

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
Los Angeles 20 C 10/10/2005

You can produce the same result, with a slightly modified SQL INSERT INTO syntax:

INSERT INTO Weather


VALUES ('Los Angeles', 20, '10/10/2005')

You are allowed to omit the list of column names in the SQL INSERT INTO clause, if you enter values for
each of the table columns.

When using SQL INSERT INTO you might not want to enter values for all columns and in this case you have
to specify the list of columns you are entering values for. If you do not enter values for all columns, then the
columns you have omitted must allow NULL values or at least have a default value defined. The following
SQL INSERT example enters only 2 of the 3 columns in the Weather table:

INSERT INTO Weather (City, Date)


VALUES ('Boston', '10/10/2005')

The result of this SQL INSERT will be as follows:

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
Los Angeles 20 C 10/10/2005
Boston NULL 10/10/2005

We've inserted a new row for Boston, but we haven't received the temperature value for 10/10/2005 that's
why we didn't enter it.
Q9 Sql update command

The SQL UPDATE clause serves to update data in database table. The SQL UPDATE clause basic syntax
looks like this:

UPDATE Table1
SET Column1 = Value1, Column2 = Value2, …

The first line of the above SQL UPDATE statement defines which table we are updating. The second line
starts with the SET SQL keyword followed by one or more Column = Value pairs separated by commas. The
second line of the UPDATE statement defines which table columns to update and with what value.

Please consider the following SQL UPDATE syntax:

UPDATE Weather
SET AverageTemperature = 20

Before we run this UPDATE SQL expression, our Weather table looks like this:

City AverageTemperature Date


New York 22 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
New York 18 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005

After the update it looks like this:

City AverageTemperature Date


New York 20 C 10/10/2005
Seattle 20 C 10/10/2005
Washington 20 C 10/10/2005
New York 20 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 20 C 10/09/2005

As you can see all values in the AverageTemperature column were set to 20.
But what if we want to update (change) only the AverageTemperature values for New York? We can do that
by using the UPDATE and the WHERE SQL clauses together:

UPDATE Weather
SET AverageTemperature = 20
WHERE City = 'New York'

The result will be:

City AverageTemperature Date


New York 20 C 10/10/2005
Seattle 21 C 10/10/2005
Washington 20 C 10/10/2005
New York 20 C 10/09/2005
Seattle 20 C 10/09/2005
Washington 17 C 10/09/2005

In some cases you might want to UPDATE a column in a table, and make the new value of the column
dependable on the old one. For example you might want to increase the AverageTemperature column
values with 5 C for all entries in the table. To do this kind of SQL UPDATE you can use the following
UPDATE statement:

UPDATE Weather
SET AverageTemperature = AverageTemperature + 5

Our SQL UPDATE statement above simply instructs says that the new value of AverageTemperature will be
equal to the old one plus 5.

The SQL UPDATE clause is very powerful and you can easily alter one or more table entries by mistake,
thus losing their original values. To avoid that make sure you update only the rows that you want, by utilizing
the SQL WHERE clause. It's a good idea to make a backup of your table before running UPDATE
statements for tables with important data.

S-ar putea să vă placă și