0% found this document useful (0 votes)
1K views

SQL Select: City Averagetemperature Date

The SQL SELECT clause selects data from one or more database tables and/or views. It includes a list of columns to select and specifies the table to extract data from. The SQL UNION clause merges the results of two or more SELECT queries into a single result set. The SQL TRUNCATE TABLE clause deletes all rows from a database table. The SQL INSERT INTO clause facilitates inserting data into a SQL table by specifying the table, columns, and values. SQL AND and OR are used with the WHERE clause to join multiple search conditions. SQL replication distributes and synchronizes data between databases, with publishers supplying data and subscribers receiving data.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

SQL Select: City Averagetemperature Date

The SQL SELECT clause selects data from one or more database tables and/or views. It includes a list of columns to select and specifies the table to extract data from. The SQL UNION clause merges the results of two or more SELECT queries into a single result set. The SQL TRUNCATE TABLE clause deletes all rows from a database table. The SQL INSERT INTO clause facilitates inserting data into a SQL table by specifying the table, columns, and values. SQL AND and OR are used with the WHERE clause to join multiple search conditions. SQL replication distributes and synchronizes data between databases, with publishers supplying data and subscribers receiving data.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL SELECT

The SQL SELECT clause selects data from one or more database tables and/or views. In its basic form the SQL SELECT syntax looks like this:

SELECT ColumnName1, ColumnName2, FROM Table1


Let's have a look at the SELECT SQL statement above. The first part starts with the SELECT clause followed by a list of columns separated by commas. This list of columns defines which columns we are selecting data from. The second part of our SQL SELECT starts with the FROM clause followed by name of table from where we are extracting data. We will use a table called Weather with 3 columns City, AverageTemperature and Date, to give a real world SQL SELECT example:

City New York Seattle Washington

AverageTemperature 22 C 21 C 20 C

Date 10/10/2005 10/10/2005 10/10/2005


To select all cities from the above table, we will use this SQL SELECT statement:

SELECT City FROM Weather


The result will be:

City New York Seattle Washington


If we want to select all the data (all columns) from the Weather table we can do it with the following SQL SELECT:

SELECT * FROM Weather


The * replaced the column list following the SELECT SQL clause, thus instructing the SQL interpreter to return all columns.

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

SQL TRUNCATE TABLE

The SQL TRUNCATE TABLE clause deletes all rows from a database table. Here is the SQL TRUNCATE TABLE syntax:

TRUNCATE TABLE Weather


The SQL TRUNCATE TABLE clause does the same as a SQL DELETE clause which doesn't have a SQL WHERE clause. The following two SQL statements are equivalent:

TRUNCATE TABLE Weather DELETE FROM Weather


Use SQL TRUNCATE TABLE only when you want to delete all rows in a table.

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 New York Seattle Washington Los Angeles

AverageTemperature 22 C 21 C 20 C 20 C

Date 10/10/2005 10/10/2005 10/10/2005 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 New York Seattle Washington Los Angeles Boston

AverageTemperature 22 C 21 C 20 C 20 C NULL

Date 10/10/2005 10/10/2005 10/10/2005 10/10/2005 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.

SQL AND & OR

The SQL AND & SQL OR keywords are used together with the SQL WHERE clause to 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 New York Seattle

AverageTemperature 22 C 21 C

Date 10/10/2005 10/10/2005

Washington New York Seattle Washington

20 C 18 C 20 C 17 C

10/10/2005 10/09/2005 10/09/2005 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 New York Washington New York Washington

AverageTemperature 22 C 20 C 18 C 17 C

Date 10/10/2005 10/10/2005 10/09/2005 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

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

You might also like