59
SQL: Queries, Schemas, and SQL: Queries, Schemas, and Joins- OH MY!!! Joins- OH MY!!! Geoff Noel

SQL: Queries, Schemas, and Joins- OH MY!!! Geoff Noel

Embed Size (px)

Citation preview

SQL: Queries, Schemas, and SQL: Queries, Schemas, and Joins- OH MY!!!Joins- OH MY!!!

Geoff Noel

You may ask You may ask yourself – Well, yourself – Well, How did I get How did I get

here….here….David Byrne – Once in a lifetimeDavid Byrne – Once in a lifetime

Databases come in all different shapes and sizes. They can be flat files of ASCII data (like Access or Q&A) or complex binary tree structures (Oracle or Sybase). In any form, a database is a data store, or a place that holds data.

If a database is simply a collection of data, then what keeps track of changes to this data?

That is the job of the database management system, or DBMS. Some DBMSs are relational. Those are RDBMS. The relational part refers to the fact that separate collections of data within the reaches of the RDBMS can be looked at together in unison. The RDBMS is responsible for ensuring the integrity of the database. Sometimes, things will get out of whack and the RDBMS will keep all that data in line.

Databases Overview

What Is a Database?What Is a Database?

What is a database and database management system.What is a database and database management system.A database is a collection of related data.A database is a collection of related data.A database management system (DBMS) is a collection of programs that enables A database management system (DBMS) is a collection of programs that enables users to create and maintain a database.users to create and maintain a database.

Why use a database management systemWhy use a database management system??To control redundancy.To control redundancy.To restrict unauthorized access.To restrict unauthorized access.To provide persistent storage for program objects and data structures.To provide persistent storage for program objects and data structures.To permit inference and actions using rules.To permit inference and actions using rules.To provide multiple user interfaces.To provide multiple user interfaces.To representing complex relationships among data.To representing complex relationships among data.To enforce integrity constraints.To enforce integrity constraints.To provide backup and recovery.To provide backup and recovery.

Database Client/Server Methodology

The evolution of relational data storage began in 1970 with the work of Dr. The evolution of relational data storage began in 1970 with the work of Dr. E. F. Codd, who proposed a set of 12 rules for identifying relationships E. F. Codd, who proposed a set of 12 rules for identifying relationships between pieces of data. Codd's rules formed the basis for the development between pieces of data. Codd's rules formed the basis for the development of systems to manage data. Today, Relational Database Management of systems to manage data. Today, Relational Database Management Systems (RDBMS) are the result of Codd's vision. Systems (RDBMS) are the result of Codd's vision.

Data in an RDBMS are stored as rows of distinct information in tables. A Data in an RDBMS are stored as rows of distinct information in tables. A structured language is used to query (retrieve), store and change the data. structured language is used to query (retrieve), store and change the data. The Structured Query Language (SQL) is an ANSI standard, and all major The Structured Query Language (SQL) is an ANSI standard, and all major commercial RDBMS vendors provide mechanisms for issuing SQL commercial RDBMS vendors provide mechanisms for issuing SQL commands. commands.

Single-Tier Database DesignSingle-Tier Database Design The early development of RDBMS applications utilized an integrated model of user interface code,The early development of RDBMS applications utilized an integrated model of user interface code,

Application code and database libraries. This single binary model ran only on a local machine,Application code and database libraries. This single binary model ran only on a local machine,

typically a mainframe. The applications were simple but inefficient and did not work over LANs. Thetypically a mainframe. The applications were simple but inefficient and did not work over LANs. The

model did not scale the application and user interface code was tightly coupled to the databasemodel did not scale the application and user interface code was tightly coupled to the database

libraries.libraries.

The monolithic single-tier database designThe monolithic single-tier database design

The two-tier database design

Two-Tier Database DesignTwo-Tier Database Design

Two-tier model appeared with the advent of server technology. Communication-protocol development andTwo-tier model appeared with the advent of server technology. Communication-protocol development andextensive use of local and wide area networks allowed the database developer to create an application frontextensive use of local and wide area networks allowed the database developer to create an application frontend that accessed data through a connection (end that accessed data through a connection (socketsocket) to the back-end server. A two-tier database design,) to the back-end server. A two-tier database design,where the client software is connected to the database through a socket connection. where the client software is connected to the database through a socket connection.

Client programs (applying a user interface) send SQL requests to the database server. The server returns the appropriate results, and the client is responsible for the formatting and display of the data. Clients still use a vendor-provided library of functions that manage the communication between client and server. Most of these libraries are written in either the C language or Perl. Commercial database vendors realized the potential for adding intelligence to the database server. They created proprietary techniques that allowed the database designer to develop macro programs for simple data manipulation. These macros, called stored procedures, can cause problems relating to version control and maintenance. Because a stored procedure is an executable program living on the database, it is possible for the stored procedure to attempt to access named columns of a database table after the table has been changed. For example, if a column with the name id is changed to cust_id, the meaning of the original stored procedure is lost. The advent of triggers, which are stored procedures executed automatically when some action (such as insert) happens with a particular table or tables, can compound these difficulties when the data returned from a query are not expected. Again, this can be the result of the trigger reading a table column that has been altered.

The vendor-provided library limits them. Switching from oneThe vendor-provided library limits them. Switching from onedatabase vendor to another requires a rewrite of a significantdatabase vendor to another requires a rewrite of a significantamount of code to the client application. amount of code to the client application.

Version control is an issue. When the vendor updates the client-sideVersion control is an issue. When the vendor updates the client-sidelibraries, the applications that utilize the database must belibraries, the applications that utilize the database must berecompiled and redistributed. recompiled and redistributed.

Vendor libraries deal with low-level data manipulation. Typically,Vendor libraries deal with low-level data manipulation. Typically,the base library only deals with queries and updates of single rowsthe base library only deals with queries and updates of single rowsor columns of data. This can be enhanced on the server side byor columns of data. This can be enhanced on the server side bycreating a stored procedure, but the complexity of the system thencreating a stored procedure, but the complexity of the system thenincreases. increases.

All of the intelligence associated with using and manipulating the data isAll of the intelligence associated with using and manipulating the data isimplemented in the client application, creating large client-side runtimes. Thisimplemented in the client application, creating large client-side runtimes. Thisdrives up the cost of each client set. drives up the cost of each client set.

Limitations of Two-Tier Database DesignLimitations of Two-Tier Database Design

Three-Tier Database DesignThree-Tier Database DesignIn a multi-tier design, the client communicates with an intermediate server that provides aIn a multi-tier design, the client communicates with an intermediate server that provides a

Layer of abstraction from the RDBMS. The intermediate layer is designed to handle multipleLayer of abstraction from the RDBMS. The intermediate layer is designed to handle multiple

Client requests and manage the connection to one or more database servers. There does notClient requests and manage the connection to one or more database servers. There does not

have to be just three tiers, but conceptually this is the next step. have to be just three tiers, but conceptually this is the next step.

Three-Tier Database DesignThree-Tier Database Design

Advantages of Three-Tier DesignAdvantages of Three-Tier Design It is multithreaded to manage multiple client connections simultaneously. It is multithreaded to manage multiple client connections simultaneously.

It can accept connections from clients over a variety of vendor-neutral It can accept connections from clients over a variety of vendor-neutral protocols (from HTTP to TCP/IP), then hand off the requests to the protocols (from HTTP to TCP/IP), then hand off the requests to the appropriate vendor-specific database servers, returning the replies to the appropriate vendor-specific database servers, returning the replies to the appropriate clients. appropriate clients.

It can be programmed with a set of "business rules" that manage the It can be programmed with a set of "business rules" that manage the manipulation of the data. Business rules could include anything from manipulation of the data. Business rules could include anything from restricting access to certain portions of data to making sure that data is restricting access to certain portions of data to making sure that data is properly formatted before being inserted or updated. properly formatted before being inserted or updated.

It prevents the client from becoming too heavy by centralizing process-It prevents the client from becoming too heavy by centralizing process-intensive tasks and abstracting data representation to a higher level. intensive tasks and abstracting data representation to a higher level.

It isolates the client application from the database system and frees a It isolates the client application from the database system and frees a company to switch database systems without having to rework the business company to switch database systems without having to rework the business rules. rules.

It can asynchronously provide the client with the status of a current data It can asynchronously provide the client with the status of a current data table or row. table or row.

Different RDBMS’ - HistoryDifferent RDBMS’ - History Different RDBMS’ - HistoryDifferent RDBMS’ - History

DB2/UDBDB2/UDBNCR TeradataNCR TeradataIngresIngresInformixInformixSybaseSybaseMySQLMySQLGupta/Centura -SQLbaseGupta/Centura -SQLbaseDBaseDBaseParadox . . . Many othersParadox . . . Many others

DB2/UDBDB2/UDBNCR TeradataNCR TeradataIngresIngresInformixInformixSybaseSybaseMySQLMySQLGupta/Centura -SQLbaseGupta/Centura -SQLbaseDBaseDBaseParadox . . . Many othersParadox . . . Many others

Other Other DatabasesDatabases

Other Other DatabasesDatabases

7.x7.x8.0.x8.0.x8.1.x ( aka 8i)8.1.x ( aka 8i)9.29.210G10G

7.x7.x8.0.x8.0.x8.1.x ( aka 8i)8.1.x ( aka 8i)9.29.210G10G

OracleOracleOracleOracle

Sybase/ SQL Server 6.0Sybase/ SQL Server 6.06.56.57.07.02000200020052005

Sybase/ SQL Server 6.0Sybase/ SQL Server 6.06.56.57.07.02000200020052005

MS SQL ServerMS SQL ServerMS SQL ServerMS SQL Server

Various Methods of ConnectionVarious Methods of ConnectionVarious Methods of ConnectionVarious Methods of Connection

ODBCODBCJDBCJDBCNativeNativeOLE/ADOOLE/ADOBCPBCPSQL-NETSQL-NETSQL-LOADERSQL-LOADERHPLHPL

ToolsToolsToolsTools

I-SQL I-SQL SQL WorksheetSQL WorksheetEnterprise Enterprise ManagerManagerToadToadDB ArtisanDB ArtisanQuery AnalyzerQuery AnalyzerWin SQLWin SQLO-SQLO-SQLSQL-PlusSQL-Plus

Enterprise Enterprise ManagerManagerDB ArtisanDB ArtisanMS QueryMS QueryAccessAccessCrystalCrystalData DictionaryData DictionaryERD (Entity ERD (Entity Relationship Relationship Diagram)Diagram)

Schema – By Object TypeSchema – By Object Type

Schema – By OwnerSchema – By Owner

SQLbase – SQL Talk utilitySQLbase – SQL Talk utility

SQLbase – SQL COnsoleSQLbase – SQL COnsoleSQLbase – SQL COnsoleSQLbase – SQL COnsole

What is a Schema ?What is a Schema ?

Pronounced -> Pronounced -> skee-maskee-ma..

The structure of a database system, described in a formal The structure of a database system, described in a formal language supported bylanguage supported by

the database management system (DBMS). In a relational the database management system (DBMS). In a relational Database (RDBMS),Database (RDBMS),

the schema defines the tables, the fields in each table, and thethe schema defines the tables, the fields in each table, and the

Relationships between fields and tables. Schemas are generally Relationships between fields and tables. Schemas are generally stored in a datastored in a data

dictionary. Although a schema is defined in text database dictionary. Although a schema is defined in text database language, the term islanguage, the term is

often used to refer to a graphical depiction of the database often used to refer to a graphical depiction of the database structure. structure.

Database ObjectsDatabase Objects DDL- data definitionDDL- data definition

TablesTables ColumnsColumns

Data-typesData-types

IndexesIndexes Primary keyPrimary key Foreign keyForeign key

ViewsViews Table-spacesTable-spaces PartitionsPartitions ConstraintsConstraints SynonymsSynonyms

DML- data manipulationDML- data manipulation PackagesPackages TriggersTriggers ProceduresProcedures FunctionsFunctions ConstraintsConstraints SequencesSequences

Database actionsDatabase actions

S – Select …….. > QueriesS – Select …….. > Queries U - UpdateU - Update I - InsertI - Insert D - DeleteD - Delete

Table BasicsTable Basics A relational database system A relational database system

contains one or more objects contains one or more objects called tables. The data or called tables. The data or information for the database are information for the database are stored in these tables. Tables are stored in these tables. Tables are uniquely identified by their names uniquely identified by their names and are comprised of columns and and are comprised of columns and rows. Columns contain the column rows. Columns contain the column name, data type, and any other name, data type, and any other attributes for the column. Rows attributes for the column. Rows contain the records or data for the contain the records or data for the columns. Here is a sample table columns. Here is a sample table called "weather".called "weather".

city, state, high, and low are the city, state, high, and low are the columns. The rows contain the columns. The rows contain the data for this table:data for this table:

Weather

city state high low

Phoenix Arizona 105 90

Tucson Arizona 101 92

Flagstaff Arizona 88 69

San Diego California 77 60

Albuquerque New Mexico 80 72

Primary Key (PK)Primary Key (PK)

A A primary keyprimary key is a table is a table column that can be used to column that can be used to uniquely identify every row of uniquely identify every row of the table. the table. Any column that Any column that has this property will dohas this property will do -- -- these columns are called these columns are called candidatecandidate keys. A table can keys. A table can have many candidate keys have many candidate keys but only one primary key. The but only one primary key. The primary key cannot be null.primary key cannot be null.

FooNumber

FirstName

LastName

BarTab

21 Fred Jones 47

32 Bill Smith 23

87 Wendy Jones -

32 Bob Stikino 943

Composite Primary KeyComposite Primary Key A is a primary key consisting of more than one column. In the above A is a primary key consisting of more than one column. In the above

example, the combinations (RecordNo,FirstName), (RecordNo,Lastname), example, the combinations (RecordNo,FirstName), (RecordNo,Lastname), (RecordNo,FirstName,Lastname), and (FirstName,LastName) are all (RecordNo,FirstName,Lastname), and (FirstName,LastName) are all candidate keys. Any combination including Age is not a candidate key candidate keys. Any combination including Age is not a candidate key because it contains a null.because it contains a null.

Often, database designers add an extra column to their table designs, a Often, database designers add an extra column to their table designs, a column defined as an integer, which will hold a number. In Microsoft column defined as an integer, which will hold a number. In Microsoft Access, this is an Access, this is an autonumberautonumber, in MySQL it's an , in MySQL it's an auto-incrementauto-increment, in Oracle , in Oracle it's a it's a sequencesequence, and in SQL/Server it's an , and in SQL/Server it's an identityidentity column. As these names column. As these names suggest, this integer is automatically assigned by the database, usually suggest, this integer is automatically assigned by the database, usually incrementally, sometimes using an initial value and increment that you can incrementally, sometimes using an initial value and increment that you can specify. Some databases allow these numbers to be generated randomly.specify. Some databases allow these numbers to be generated randomly.

The purpose of this type of automatically generated number is to act as the The purpose of this type of automatically generated number is to act as the surrogatesurrogate primary key, usually in those situations similar to the above primary key, usually in those situations similar to the above where candidate keys are multi-column. The awkwardness of a multi-where candidate keys are multi-column. The awkwardness of a multi-column candidate key becomes apparent as soon as you define a foreign column candidate key becomes apparent as soon as you define a foreign key on it.key on it.

A A foreign keyforeign key is a column, or is a column, or combination of columns, that contain combination of columns, that contain values that are found in the primary key values that are found in the primary key of some table (including, possibly, itself). of some table (including, possibly, itself). A foreign key may be null, and almost A foreign key may be null, and almost always is not unique.That last statement always is not unique.That last statement may be counterintuitive, so let's take may be counterintuitive, so let's take another example. Here we have two another example. Here we have two tables that are tables that are relatedrelated via a foreign key – via a foreign key –

As you can see, the ClrFK column in the As you can see, the ClrFK column in the second table is a foreign key to the ClrPK second table is a foreign key to the ClrPK primary key in the first table. Notice that primary key in the first table. Notice that the ClrPK values are unique and not null, the ClrPK values are unique and not null, but the ClrFK values may be null and but the ClrFK values may be null and often repeat. A null foreign key means often repeat. A null foreign key means that that that particular rowthat particular row does not does not participate in the relationship. The fact participate in the relationship. The fact that many foreign key values repeat that many foreign key values repeat simply reflects the fact that it's a simply reflects the fact that it's a one-to-one-to-manymany relationship. relationship.

In a one-to-many relationship, the In a one-to-many relationship, the primary key has the "one" value, and the primary key has the "one" value, and the foreign key has the "many" values. The foreign key has the "many" values. The trick to remembering this is to keep in trick to remembering this is to keep in mind that the primary key must be mind that the primary key must be unique. unique.

ClrPK ColourName

10 yellow

20 red

30 green

40 blue

AdjPK AdjectiveName

ClrFK

904 angry 20

913 envious 30

937 lazy -

941 lonely 40

954 fearful 10

979 jealous 30

991 furious 20

Foreign Key (FK)Foreign Key (FK)

It Ain’t nothing – it’s NULLIt Ain’t nothing – it’s NULL

Null means either "don't know" or "not Null means either "don't know" or "not applicable" -- it's not really the same as applicable" -- it's not really the same as zero or any other default value, but zero or any other default value, but more importantly, null is treated quite more importantly, null is treated quite differently from other values in SQL, differently from other values in SQL, because it literally has no value.because it literally has no value.

Here's an example of a "don't know" Here's an example of a "don't know" null ->null ->

As you can see, Fred's value is null, As you can see, Fred's value is null, which you could interpret as meaning which you could interpret as meaning that Fred didn't take the test (maybe that Fred didn't take the test (maybe he has a medical exemption and will he has a medical exemption and will take the test another day). It would be take the test another day). It would be wrong to assign a zero, because that wrong to assign a zero, because that would be interpreted as Fred having would be interpreted as Fred having taken the test and not getting a single taken the test and not getting a single answer right! answer right!

Student TestResult

Joe 87

Bill 73

Mary 56

Fred null

Sam 92

Now consider the following query –Now consider the following query –

SELECT AVG(TestResult) FROM Students;SELECT AVG(TestResult) FROM Students;

Aggregate functions like AVG() and SUM() ignore nulls, so this query will return (87+73+56+92)/4=77,which is certainly better than 87+73+56+0+92)/5=61.6 which you'd get using a zero default. Often a defaultvalue is just wrong for a column where you expect to take aggregates.

An example of a column that would take a "not applicable" null is Date Terminated in a human resourcesAn example of a column that would take a "not applicable" null is Date Terminated in a human resourcesdatabase, where the value would be null for all active employees. To test for nulls, you can filter them out indatabase, where the value would be null for all active employees. To test for nulls, you can filter them out inthe WHERE clause –the WHERE clause –

SELECT EmployeeID , (DateTerminated - DateHired) AS LengthOfService FROM EmployeeTable SELECT EmployeeID , (DateTerminated - DateHired) AS LengthOfService FROM EmployeeTable WHERE DateTerminated IS NOT NULLWHERE DateTerminated IS NOT NULL

which would give results only for terminated employees. If you didn't have the WHERE clause, the abovewhich would give results only for terminated employees. If you didn't have the WHERE clause, the abovequery would return null for every active employee, because any expression involving a null yields a nullquery would return null for every active employee, because any expression involving a null yields a nullresult.result.

Alternatively, you can use the COALESCE function to supply a non-null value –Alternatively, you can use the COALESCE function to supply a non-null value –

SELECT EmployeeID , ( COALESCE(DateTerminated,GETDATE()) - DateHired) AS LengthOfService SELECT EmployeeID , ( COALESCE(DateTerminated,GETDATE()) - DateHired) AS LengthOfService FROM EmployeeTable where GETDATE()FROM EmployeeTable where GETDATE()

returns today's date and therefore provides an accurate measure for the length of service of activereturns today's date and therefore provides an accurate measure for the length of service of activeemployees. So for terminated employees, DateTerminated is not null, and the calculation is the same asemployees. So for terminated employees, DateTerminated is not null, and the calculation is the same asabove, while for active employees, DateTerminated is null so COALESCE uses today's date instead.above, while for active employees, DateTerminated is null so COALESCE uses today's date instead.

What SQL?What SQL? SQL or SEQUEL ?SQL or SEQUEL ?

SQL (pronounced "ess-que-el") SQL (pronounced "ess-que-el")

The acronym SQL stands for The acronym SQL stands for SStructured tructured QQuery uery LLanguageanguage

SQL is used to communicate with a database. According to ANSI (American National SQL is used to communicate with a database. According to ANSI (American National Standards Institute), it is the standard language for relational database management Standards Institute), it is the standard language for relational database management systems. SQL statements are used to perform tasks such as update data on a database, systems. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management or retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc. systems that use SQL are: Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc. Although most database systems use SQL, most of them also have their own additional Although most database systems use SQL, most of them also have their own additional proprietary extensions that are usually only used on their system. However, the standard proprietary extensions that are usually only used on their system. However, the standard SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" can SQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" can be used to accomplish almost everything that one needs to do with a database. be used to accomplish almost everything that one needs to do with a database.

Some people claim that this is a bad name, becauseSome people claim that this is a bad name, because SQL isn't (properly) structured SQL isn't (properly) structured it's more than just queries (e.g. insert, update, delete) it's more than just queries (e.g. insert, update, delete) it isn't a real computing language (Very Debatable)it isn't a real computing language (Very Debatable)

In any case, SQL is a database query language that was adopted as an industry standard In any case, SQL is a database query language that was adopted as an industry standard in 1986. It has undergone two important revisions, SQL2 (also called SQL-92), and SQL3 in 1986. It has undergone two important revisions, SQL2 (also called SQL-92), and SQL3 (also called SQL-99).(also called SQL-99).

Selecting DataSelecting Data The The selectselect statement is used to query statement is used to query

the database and retrieve selected data the database and retrieve selected data that match the criteria that you specify. that match the criteria that you specify. Here is the format of a simple select Here is the format of a simple select statement:statement:

select "column1" [,"column2",etc] select "column1" [,"column2",etc] from "tablename" [where "condition"];from "tablename" [where "condition"];

[ ] = optional[ ] = optional The column names that follow the The column names that follow the

select keyword determine which select keyword determine which columns will be returned in the results. columns will be returned in the results. You can select as many column names You can select as many column names that you'd like, or you can use a "*" to that you'd like, or you can use a "*" to select all columns.select all columns.

The table name that follows the The table name that follows the keyword keyword fromfrom specifies the table that specifies the table that will be queried to retrieve the desired will be queried to retrieve the desired results.results.

The The wherewhere clause (optional) specifies clause (optional) specifies which data values or rows will be which data values or rows will be returned or displayed, based on the returned or displayed, based on the criteria described after the keyword criteria described after the keyword wherewhere..

Conditional selections used in the Conditional selections used in the wherewhere clause: clause:

= Equal

> Greater than

< Less than

>= Greater than or equal

<= Less than or equal

<> Not equal to

LIKE *See next page – Special Operator

select first, last, city from empinfo; select first, last, city from empinfo; select last, city, age from empinfo select last, city, age from empinfo where age > 30; where age > 30;

select first, last, city, state from select first, last, city, state from empinfo where first LIKE 'J%'; empinfo where first LIKE 'J%';

select * from empinfo; select * from empinfo;

select first, last, from empinfo select first, last, from empinfo where last LIKE '%s'; where last LIKE '%s';

select first, last, age from empinfo select first, last, age from empinfo where last LIKE '%illia%'; where last LIKE '%illia%';

select * from empinfo where first = select * from empinfo where first = 'Eric';'Eric';

Sample Table: empinfo

first last idag

ecity state

John Jones9998

045 Payson Arizona

Mary Jones9998

225 Payson Arizona

EricEdward

s8823

232 San Diego

California

Mary Ann

Edwards

88233

32 Phoenix Arizona

Ginger Howell9800

242

Cottonwood

Arizona

Sebastian

Smith9200

123 Gila Bend Arizona

Gus Gray2232

235 Bagdad Arizona

Mary Ann

May3232

652 Tucson Arizona

EricaWilliam

s3232

760 Show Low Arizona

Leroy Brown3238

022 Pinetop Arizona

Elroy Cleaver3238

222 Globe Arizona

select first, last, city from select first, last, city from empinfo; select last, city, age empinfo; select last, city, age from empinfo where age > 30; from empinfo where age > 30;

Sample Table: empinfo

first last idag

ecity state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

EricEdward

s88232 32 San Diego

California

Mary AnnEdward

s88233 32 Phoenix Arizona

Ginger Howell 98002 42Cottonwoo

dArizona

Sebastian

Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

last city age

Jones Payson 45

Edwards San Diego 32

Edwards Phoenix 32

Howell Cottonwood 42

Gray Bagdad 35

May Tucson 52

Williams Show Low 60

select first, last, city, state select first, last, city, state from empinfo where first LIKE from empinfo where first LIKE 'J%'; 'J%'; Sample Table: empinfo

first last id age city state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

first last city state

John Jones Payson Arizona

select * from empinfo; select * from empinfo; Sample Table: empinfo

first last idag

ecity state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

first last id age city state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

select first, last, from empinfo select first, last, from empinfo where last LIKE '%s'; where last LIKE '%s';

Sample Table: empinfo

first last id age city state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

first last

John Jones

Mary Jones

Eric Edwards

Mary Ann Edwards

Erica Williams

select first, last, age from select first, last, age from empinfo where last LIKE empinfo where last LIKE '%illia%'; '%illia%';

Sample Table: empinfo

first last id age city state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

first last age

Erica Williams 60

select * from empinfo where first select * from empinfo where first = 'Eric';= 'Eric';

Sample Table: empinfo

first last id age city state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

first last id age city state

Eric Edwards 88232 32 San Diego California

The The LIKELIKE pattern matching operator can also be pattern matching operator can also beused in the conditional selection of the whereused in the conditional selection of the whereclause. Like is a very powerful operator that allowsclause. Like is a very powerful operator that allowsyou to select only rows that are "like" what youyou to select only rows that are "like" what youspecify. The percent sign "%“ can be used as specify. The percent sign "%“ can be used as wild card to match any possible characterwild card to match any possible characterthat might appear before or after the charactersthat might appear before or after the charactersspecified.specified.

For example:For example:

select first, last, city from empinfo whereselect first, last, city from empinfo whereFirst LIKE 'Er%';First LIKE 'Er%';

This SQL statement will match any first namesThis SQL statement will match any first namesthat start with 'Er'.that start with 'Er'.

Strings must be in single quotes.Strings must be in single quotes.

Or you can specify,Or you can specify,select first, last from empinfo where last LIKE select first, last from empinfo where last LIKE

'%s';'%s';

This statement will match any last names that end This statement will match any last names that end in a 's'.in a 's'.

select * from empinfo where first = 'Eric'; select * from empinfo where first = 'Eric';

This will only select rows where the first name This will only select rows where the first name equals 'Eric' exactly.equals 'Eric' exactly.

Sample Table: empinfo

first last id age city state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Elroy Cleaver 32382 22 Globe Arizona

The JOIN conceptThe JOIN concept

JOIN is a query clause that can be used with the SELECT, UPDATE, and DELETEdata query statements to simultaneously affect rows from multiple tables. Thereare several distinct types of JOIN statements that return different data result sets.

Joined tables must each include at least one field in both tables that containcomparable data. For example, if you want to join a Customer table and aTransaction table, they both must contain a common element, such as CustomerID column, to serve as a key on which the data can be matched. Tablescan be joined on multiple columns so long as the columns have the potential tosupply matching information. Column names across tables don't have to be thesame, although for readability this standard is generally preferred.

When you do use like column names in multiple tables, you must use fully qualified column names. This is a “dot” notation that combines the names of tables and columns. For example, if I have two tables, Customer and Transaction, and they both contain the column CustomerID, I’d use the dot notation, as in Customer.CustomerID and Transaction.CustomerID, to let the database know which column from which table I’m referring.

Now that we’ve examined the basic theory, let’s take a look at the various types ofjoins and examples of each.

The basic JOIN statementThe basic JOIN statement

A basic JOIN statement has the following format:A basic JOIN statement has the following format:

SELECT Customer.CustomerID, TransID, TransAmt FROM Customer JOIN Transaction SELECT Customer.CustomerID, TransID, TransAmt FROM Customer JOIN Transaction ON Customer.CustomerID = Transaction.CustomerID;ON Customer.CustomerID = Transaction.CustomerID;

In practice, you'd never use the example above because the type of join is not specified. In this case,In practice, you'd never use the example above because the type of join is not specified. In this case,SQL Server assumes an INNER JOIN. You can get the equivalent to this query by using the SQL Server assumes an INNER JOIN. You can get the equivalent to this query by using the statement:statement:

SELECT Customer.CustomerID, TransID, TransAmt FROM Customer, Transaction;SELECT Customer.CustomerID, TransID, TransAmt FROM Customer, Transaction;

However, the example is useful to point out a few noteworthy concepts:However, the example is useful to point out a few noteworthy concepts:

TransIDTransID and and TransAmtTransAmt do not require fully qualified names because they exist in only one of the tables. do not require fully qualified names because they exist in only one of the tables. You can use fully qualified names for readability if you wish. You can use fully qualified names for readability if you wish.

The The CustomerCustomer table is considered to be the “left” table because it was called first. Likewise, the table is considered to be the “left” table because it was called first. Likewise, the TransactionTransaction table is the “right” table. table is the “right” table.

You can use more than two tables, in which case each one is “naturally” joined to the cumulative result in You can use more than two tables, in which case each one is “naturally” joined to the cumulative result in the order they are listed, unless controlled by other functionality such as “join hints” or parenthesis. the order they are listed, unless controlled by other functionality such as “join hints” or parenthesis.

You may use WHERE and ORDER BY clauses with any JOIN statement to limit the scope of your results. You may use WHERE and ORDER BY clauses with any JOIN statement to limit the scope of your results. Note that these clauses are applied to the results of your JOIN statement. Note that these clauses are applied to the results of your JOIN statement.

SQL Server does not recognize the semicolon (;), but I use it in the included examples to denote the end SQL Server does not recognize the semicolon (;), but I use it in the included examples to denote the end of each statement, as would be expected by most other RDBMSs.of each statement, as would be expected by most other RDBMSs.

Another addition to your SQL toolbox Although the JOIN statement is often perceived as aAnother addition to your SQL toolbox Although the JOIN statement is often perceived as acomplicated concept, you will see that it’s a powerful timesaving resource that’s relatively easy tocomplicated concept, you will see that it’s a powerful timesaving resource that’s relatively easy tounderstand. Use this functionality to get related information from multiple tables with a single query understand. Use this functionality to get related information from multiple tables with a single query and to skillfully reference normalized data. Once you’ve mastered JOINs, you can elegantly maneuver and to skillfully reference normalized data. Once you’ve mastered JOINs, you can elegantly maneuver within even the most complex databasewithin even the most complex database..

Inner joinInner join

In In relational databases, a , a join operationjoin operation matches records in two tables. The two matches records in two tables. The two tables must be tables must be joinedjoined by at least one by at least one common common field. That is, the . That is, the join field join field is a is a member of both tables. Typically, a join member of both tables. Typically, a join operation is part of a SELECT operation is part of a SELECT query. .

select * from A, B where A.x = B.yselect * from A, B where A.x = B.y

The column names (x and y in this example) The column names (x and y in this example) are often, but not necessarily, the same. are often, but not necessarily, the same.

Outer JoinOuter Join(database)outer join - A less commonly used variant of the (database)outer join - A less commonly used variant of the inner join relational database operation. An inner join selects operation. An inner join selects rows from two tables such that the value in one column of rows from two tables such that the value in one column of the first table also appears in a certain column of the the first table also appears in a certain column of the second table. For an outer join, the result also includes all second table. For an outer join, the result also includes all rows from the first operand ("left outer join", "*="), or the rows from the first operand ("left outer join", "*="), or the second operand ("right outer join", "=*"), or both ("full second operand ("right outer join", "=*"), or both ("full outer join", "*=*"). A field in a result row will be null if the outer join", "*=*"). A field in a result row will be null if the corresponding input table did not contain a matching row.corresponding input table did not contain a matching row.

For example, if we want to list all employees and their For example, if we want to list all employees and their employee number, but not all employees have a number, employee number, but not all employees have a number, then we could say (in then we could say (in SQL):):

SELECT employee.name, empnum.number WHERE SELECT employee.name, empnum.number WHERE employee.id *= empnum.idemployee.id *= empnum.id

The "*=" means "left outer join" and means that all rows The "*=" means "left outer join" and means that all rows from the "employee" table will appear in the result, even if from the "employee" table will appear in the result, even if there is no match for their ID in the empnum table.there is no match for their ID in the empnum table.

The notorious CROSS JOINThe notorious CROSS JOIN

The CROSS JOIN has earned a bad reputation because it’s very resource The CROSS JOIN has earned a bad reputation because it’s very resource intensive and returns results of questionable usefulness. When you use the CROSS JOIN, intensive and returns results of questionable usefulness. When you use the CROSS JOIN, you're given a result set containing every possible combination of the rows returned from you're given a result set containing every possible combination of the rows returned from each table. Take the following example:each table. Take the following example:

SELECT CustomerName, TransDate, TransAmt FROM Customer CROSS JOIN SELECT CustomerName, TransDate, TransAmt FROM Customer CROSS JOIN Transaction;Transaction;

With the CROSS JOIN, you aren’t actually free to limit the results, but you can use the With the CROSS JOIN, you aren’t actually free to limit the results, but you can use the ORDER BY ORDER BY

clause to control the way they are returned. If the tables joined in this example contained clause to control the way they are returned. If the tables joined in this example contained only five only five

rows each, you would get 25 rows of results. Every CustomerName would be listed as rows each, you would get 25 rows of results. Every CustomerName would be listed as associated with associated with

every TransDate and TransAmt.every TransDate and TransAmt.

I really did try to come up with examples where this function was useful, and they were all I really did try to come up with examples where this function was useful, and they were all very very

contrived. However, I’m sure someone out there is generating lists of all their products in contrived. However, I’m sure someone out there is generating lists of all their products in all possible all possible

colors or something similar, or we wouldn’t have this wonderful but dangerous feature.colors or something similar, or we wouldn’t have this wonderful but dangerous feature.

The INNER JOIN drops The INNER JOIN drops rowsrows

When you perform an INNER JOIN, only rows that match up are returned. Any When you perform an INNER JOIN, only rows that match up are returned. Any time a row from either table doesn’t have corresponding values from the other time a row from either table doesn’t have corresponding values from the other table, it is disregarded. Because stray rows aren’t included, you don’t have table, it is disregarded. Because stray rows aren’t included, you don’t have

any of any of the “left” and “right” nonsense to deal with and the order in which you the “left” and “right” nonsense to deal with and the order in which you

present present tables matters only if you have more than two to compare. Since this is a tables matters only if you have more than two to compare. Since this is a

simple simple concept, here’s a simple example:concept, here’s a simple example:

SELECT CustomerName, TransDate FROM Customer INNER JOIN SELECT CustomerName, TransDate FROM Customer INNER JOIN Transaction ON Customer.CustomerID = Transaction.CustomerID; Transaction ON Customer.CustomerID = Transaction.CustomerID;

If a row in the If a row in the TransactionTransaction table contains a table contains a CustomerIDCustomerID that’s not listed in the that’s not listed in the CustomerCustomer table, that row will not be returned as part of the result set. table, that row will not be returned as part of the result set.

Likewise, if Likewise, if the the CustomerCustomer table has a table has a CustomerIDCustomerID with no corresponding rows in the with no corresponding rows in the

TransactionTransaction table, the table, the row from the row from the CustomerCustomer table won’t be returned. table won’t be returned.

The OUTER JOINThe OUTER JOIN can include mismatched rowscan include mismatched rows

OUTER JOINs, sometimes called “complex joins,” aren’t actually complicated. They are so-called OUTER JOINs, sometimes called “complex joins,” aren’t actually complicated. They are so-called because SQL Server performs two functions for each OUTER JOIN.because SQL Server performs two functions for each OUTER JOIN.

The first function performed is an INNER JOIN. The second function includes the rows that the INNERThe first function performed is an INNER JOIN. The second function includes the rows that the INNERJOIN would have dropped. Which rows are included depends on the type of OUTER JOIN that is used JOIN would have dropped. Which rows are included depends on the type of OUTER JOIN that is used and the order the tables were presented.and the order the tables were presented.

There are three types of an OUTER JOIN: LEFT, RIGHT, and FULL. As you’ve probably guessed, theThere are three types of an OUTER JOIN: LEFT, RIGHT, and FULL. As you’ve probably guessed, theLEFT OUTER JOIN keeps the stray rows from the “left” table (the one listed first in your query LEFT OUTER JOIN keeps the stray rows from the “left” table (the one listed first in your query statement). In the result set, columns from the other table that have no corresponding data are filled statement). In the result set, columns from the other table that have no corresponding data are filled with NULL values. Similarly, the RIGHT OUTER JOIN keeps stray rows from the right table, filling with NULL values. Similarly, the RIGHT OUTER JOIN keeps stray rows from the right table, filling columns from the left table with NULL values. The FULL OUTER JOIN keeps all stray rows as part of columns from the left table with NULL values. The FULL OUTER JOIN keeps all stray rows as part of the result set. Here is your example:the result set. Here is your example:

SELECT CustomerName, TransDate, TransAmt FROM Customer LEFT OUTER JOIN Transaction ON SELECT CustomerName, TransDate, TransAmt FROM Customer LEFT OUTER JOIN Transaction ON Customer.CustomerID = Transaction.CustomerID;Customer.CustomerID = Transaction.CustomerID;

Customer names that have no associated transactions will still be displayed. However, transactions Customer names that have no associated transactions will still be displayed. However, transactions with no corresponding customers will not, because we used a LEFT OUTER JOIN and the with no corresponding customers will not, because we used a LEFT OUTER JOIN and the CustomerCustomer table was listed first.table was listed first.

In SQL Server, the word OUTER is actually optional. In SQL Server, the word OUTER is actually optional.

The clauses LEFT JOIN, RIGHT JOIN, and FULL JOIN are equivalent to LEFT OUTER JOIN, RIGHT OUTER The clauses LEFT JOIN, RIGHT JOIN, and FULL JOIN are equivalent to LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN, respectively.JOIN, and FULL OUTER JOIN, respectively.

SQL SubquerySQL Subquery

It is possible to embed a SQL statement within It is possible to embed a SQL statement within another. When this is done on the another. When this is done on the WHEREWHERE or the or the HAVINGHAVING statements, we have a subquery construct. statements, we have a subquery construct. What is subquery useful for? First, it can also be used What is subquery useful for? First, it can also be used to join tables. Also, there are cases where the only to join tables. Also, there are cases where the only way to correlate two tables is through a subquery. way to correlate two tables is through a subquery.

The syntax is as follows: The syntax is as follows: SELECT "column_name1" SELECT "column_name1"

FROM "table_name" FROM "table_name" WHERE "column_name2" [Comparison Operator] WHERE "column_name2" [Comparison Operator] (SELECT "column_name1" (SELECT "column_name1" FROM "table_name"FROM "table_name"WHERE [Condition]) WHERE [Condition])

[Comparison Operator] could be equality operators [Comparison Operator] could be equality operators such as =, >, <, >=, <=. It can also be a text operator such as =, >, <, >=, <=. It can also be a text operator such as "LIKE." such as "LIKE."

Let's use the same example as we did to illustrate SQL Let's use the same example as we did to illustrate SQL joins: joins:

store_name Sales Date

Los Angeles $1500 Jan-05-1999

San Diego $250 Jan-07-1999

Los Angeles $300 Jan-08-1999

Boston $700 Jan-08-1999

region_name store_name

East Boston

East New York

West Los Angeles

West San Diego

Table Store_Information Table Geography

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_InformationWHERE Store_name IN(SELECT store_name FROM Geography WHERE region_name = 'West')

SUM(Sales)

2050

In this example, instead of joining the two tables directly and then adding up only the sales amount for stores in the West region, we first use the subquery to find out which stores are in the West region, and then we sum up the sales amount for these stores.

select 'ALTER '||OBJECT_TYPE||‘ '||OWNER||'.'||OBJECT_NAME||' compile;' from select 'ALTER '||OBJECT_TYPE||‘ '||OWNER||'.'||OBJECT_NAME||' compile;' from

dba_objects where status='INVALID';dba_objects where status='INVALID';

'ALTER'||OBJECT_TYPE||''||OWNER||'.'||OBJECT_NAME||'COMPILE;'ALTER PROCEDURE MAXDATA.P_REPLACE_PROD compile;ALTER PROCEDURE MAXDATA.P_SETPROTOTYPE compile;ALTER PROCEDURE MAXDATA.UPDATE_LV10MAST compile;ALTER PROCEDURE MAXAPP.P_COMPPROC compile;

Microsoft SQL Server Reporting ServicesMicrosoft SQL Server Reporting Services

Originally not to be available until the Yukon release of SQL Server, Microsoft decided to release Originally not to be available until the Yukon release of SQL Server, Microsoft decided to release Reporting Services early because of the customer excitement they heard. Why the excitement? Reporting Services early because of the customer excitement they heard. Why the excitement? Reporting Services fills a need that many organizations are faced with—the need to build Reporting Services fills a need that many organizations are faced with—the need to build business intelligence and reporting solutions. Until now, developers were required to embed business intelligence and reporting solutions. Until now, developers were required to embed reports into their applications, or organizations were required to purchase expensive and reports into their applications, or organizations were required to purchase expensive and sometimes problematic third-party reporting solutions. Now, Reporting Services offer a complete sometimes problematic third-party reporting solutions. Now, Reporting Services offer a complete solution for distributing reports across the enterprise; enabling businesses to make decisions solution for distributing reports across the enterprise; enabling businesses to make decisions better and faster.better and faster.

Overview of Reporting ServicesOverview of Reporting ServicesReporting Services is a scalable, secure, robust reporting solution for SQL Server. It supports the Reporting Services is a scalable, secure, robust reporting solution for SQL Server. It supports the complete reporting lifecycle by including tools for report creation, execution, distribution, and complete reporting lifecycle by including tools for report creation, execution, distribution, and management. New users can have Reporting Services installed and new reports published within management. New users can have Reporting Services installed and new reports published within a matter of hours instead of days or weeks.a matter of hours instead of days or weeks.

Reporting Services consists of the following key components:Reporting Services consists of the following key components:

Report DesignerReport Designer: Supports the report creation phase of the report lifecycle. It is an add-on tool : Supports the report creation phase of the report lifecycle. It is an add-on tool for any edition of Visual Studio .NET 2003, suitable for both programmers and non-programmers. for any edition of Visual Studio .NET 2003, suitable for both programmers and non-programmers.

Report ServerReport Server: Provides services for execution and distribution of reports. : Provides services for execution and distribution of reports. Report ManagerReport Manager: A Web-based administration tool for managing the Report Server.: A Web-based administration tool for managing the Report Server.

Report DesignerReport DesignerReport Designer is a Visual Studio .NET 2003 add-on and is included with Reporting Services Report Designer is a Visual Studio .NET 2003 add-on and is included with Reporting Services (see (see Figure AFigure A). As the name implies, it provides developers and non-developers an intuitive tool ). As the name implies, it provides developers and non-developers an intuitive tool to create sophisticated reports. Users get standard reporting functionality such as grouping, to create sophisticated reports. Users get standard reporting functionality such as grouping, sorting, and report formatting. This should be sufficient for most reporting needs. For more sorting, and report formatting. This should be sufficient for most reporting needs. For more advanced reports, the Report Designer has full VB.NET support. Plus, designers can add ActiveX advanced reports, the Report Designer has full VB.NET support. Plus, designers can add ActiveX controls to their reports to create rich, live, interactive reports.controls to their reports to create rich, live, interactive reports.

One of the more compelling features of Report Designer is the ability to have dynamic, query-based One of the more compelling features of Report Designer is the ability to have dynamic, query-based parameters. This eliminates the administrator having to maintain parameter lists for all the reports (i.e., parameters. This eliminates the administrator having to maintain parameter lists for all the reports (i.e., department names, office locations, employee names, etc.). You simply have to create a new dataset department names, office locations, employee names, etc.). You simply have to create a new dataset and tie the results to the parameter. It even allows cascading parameters.and tie the results to the parameter. It even allows cascading parameters.

At the heart of Reporting Services architecture is the Report Definition Language (RDL), which is an At the heart of Reporting Services architecture is the Report Definition Language (RDL), which is an XML-based standard for defining reports. RDL is key to the Reporting Services success by allowing XML-based standard for defining reports. RDL is key to the Reporting Services success by allowing third parties to publish reports to the Reporting Server. There are already product offerings from third parties to publish reports to the Reporting Server. There are already product offerings from independent software vendors (ISVs) today.independent software vendors (ISVs) today.

Though Reporting Services requires SQL Server as its repository, Report Designer can connect to all Though Reporting Services requires SQL Server as its repository, Report Designer can connect to all types of data sources including OLE DB, ODBC, Oracle, SQL Server, and others. It also has many types of data sources including OLE DB, ODBC, Oracle, SQL Server, and others. It also has many rendering options such as HTML, Microsoft Excel, PDF, CSV, XML, and others. The list can also be rendering options such as HTML, Microsoft Excel, PDF, CSV, XML, and others. The list can also be extended by third parties or by using the Reporting Services extension library.extended by third parties or by using the Reporting Services extension library.

Report ServerReport ServerThe Report Server provides the repository, management, execution, and delivery functions. It is The Report Server provides the repository, management, execution, and delivery functions. It is scalable and secure, and can support the most demanding reporting needs. It consists of several scalable and secure, and can support the most demanding reporting needs. It consists of several subcomponents, including:subcomponents, including:

Request HandlerRequest Handler: Handles all inbound server requests and routes them to the appropriate : Handles all inbound server requests and routes them to the appropriate component. component.

Scheduling and Delivery ProcessorScheduling and Delivery Processor: Provides the scheduling and delivering functionality, and it can : Provides the scheduling and delivering functionality, and it can be extended to deliver reports to other devices such as fax machines or printers. be extended to deliver reports to other devices such as fax machines or printers.

Report ProcessorReport Processor: Provides the execution functionality and it, too, can be extended to render to new : Provides the execution functionality and it, too, can be extended to render to new output formats such as a Microsoft Word document. output formats such as a Microsoft Word document.

Report Server DatabaseReport Server Database: All the data required by Reporting Services is stored in the Report Server : All the data required by Reporting Services is stored in the Report Server Database, which must be a SQL Server. This includes everything from server settings to report Database, which must be a SQL Server. This includes everything from server settings to report definitions, even to cached data from a report execution.definitions, even to cached data from a report execution.

Report ManagerReport ManagerReport Manager provides administrators an easy-to-use tool for configuring the server and managing Report Manager provides administrators an easy-to-use tool for configuring the server and managing the reports. Using Report Manager, administrators can configure security, change server settings, the reports. Using Report Manager, administrators can configure security, change server settings, schedule reports for execution, and maintain the structure of the report folders (see schedule reports for execution, and maintain the structure of the report folders (see Figure BFigure B).).

Administrators have a variety of options for executing reports, including on-demand, cached reportingAdministrators have a variety of options for executing reports, including on-demand, cached reportingwith an adjustable expiration period, and flexible report scheduling. All of these options arewith an adjustable expiration period, and flexible report scheduling. All of these options areconfigurable at the report level through Report Manager.configurable at the report level through Report Manager.

Report Manager supports both push and pull distribution options. For e-mail delivery, Report Manager Report Manager supports both push and pull distribution options. For e-mail delivery, Report Manager can include a link to the report, attach the report to the message, or embed reports directly into the can include a link to the report, attach the report to the message, or embed reports directly into the message using Web archive. This eliminates one extra step for the reader of the report.message using Web archive. This eliminates one extra step for the reader of the report.

Where you need Reporting ServicesWhere you need Reporting Services

Here are a few typical scenarios where Reporting Services will be invaluable:Here are a few typical scenarios where Reporting Services will be invaluable:

New application developmentNew application development: : Most applications have reporting requirements that are sometimes very complex. With Reporting Most applications have reporting requirements that are sometimes very complex. With Reporting services, the business analyst, who usually has a better understanding of what is required, is able to services, the business analyst, who usually has a better understanding of what is required, is able to fulfill these requirements. This frees a developer to perform other development tasks and not develop fulfill these requirements. This frees a developer to perform other development tasks and not develop tedious, time-consuming reports. Rarely do I meet a developer who enjoys writing reports. tedious, time-consuming reports. Rarely do I meet a developer who enjoys writing reports. Furthermore, these reports will be easier to maintain and support. Furthermore, these reports will be easier to maintain and support.

Existing applicationsExisting applications: : Because of the complexity and time required to embed reports into applications, reports are often Because of the complexity and time required to embed reports into applications, reports are often created outside of the application using third-party tools and distributed manually or through batch created outside of the application using third-party tools and distributed manually or through batch jobs. By using Reporting Services, these applications can easily be extended to include a complete jobs. By using Reporting Services, these applications can easily be extended to include a complete reporting solution, embedded within the application. reporting solution, embedded within the application.

Executive dashboardExecutive dashboardExecutive dashboard is the buzzword for providing executives a comprehensive view of their Executive dashboard is the buzzword for providing executives a comprehensive view of their

business, business, commonly in an Enterprise Portal. Reporting Services includes several key features for an executive commonly in an Enterprise Portal. Reporting Services includes several key features for an executive dashboard, such as My Reports, My Subscriptions, push/pull delivery, numerous rendering options, dashboard, such as My Reports, My Subscriptions, push/pull delivery, numerous rendering options, and support for Web services.and support for Web services.

Though very powerful, there are a couple of scenarios that would not be suitable for Reporting Though very powerful, there are a couple of scenarios that would not be suitable for Reporting Services:Services:

Applications using third-party reporting solutionsApplications using third-party reporting solutions: : Reporting Services supply only a migration tool Reporting Services supply only a migration tool for Microsoft Access. If you have a significant investment in another tool and it supports your needs, for Microsoft Access. If you have a significant investment in another tool and it supports your needs, you’ll need to carefully consider the costs of migrating to Reporting Services. you’ll need to carefully consider the costs of migrating to Reporting Services.

Enterprise reporting solutionEnterprise reporting solution: : Though non-programmers can be very productive with Report Designer, there are reporting tools Though non-programmers can be very productive with Report Designer, there are reporting tools absent from Report Designer that experienced report creators will miss. For example, Reporting absent from Report Designer that experienced report creators will miss. For example, Reporting Services does not include a database abstraction layer to hide database details from users. This Services does not include a database abstraction layer to hide database details from users. This abstraction is very useful if you want to deploy a reporting solution to a large, semi-technical abstraction is very useful if you want to deploy a reporting solution to a large, semi-technical

audience.audience.

Dirty Little Secret … Dirty Little Secret … SHHH!!SHHH!!

Whether you are writing a simple database in Access/Visual Basic or a huge client-Whether you are writing a simple database in Access/Visual Basic or a huge client-server web application, SQL is a powerful tool that is a must-have skill for any server web application, SQL is a powerful tool that is a must-have skill for any developer. For a beginner it is yet another skill you need to learn, but there is a developer. For a beginner it is yet another skill you need to learn, but there is a shortcut that will allow you to write SQL without knowing SQL, and as a result of shortcut that will allow you to write SQL without knowing SQL, and as a result of using these shortcuts you will actually learn to write native SQL. “So what are these using these shortcuts you will actually learn to write native SQL. “So what are these shortcuts?”, I hear you cry, well, read on and find out.shortcuts?”, I hear you cry, well, read on and find out.

All good database and report writing applications allow you to create database All good database and report writing applications allow you to create database queries visually by deciding which tables to include, the relationship between tables, queries visually by deciding which tables to include, the relationship between tables, the sort order and the criteria of which records to include or not to include. Certain the sort order and the criteria of which records to include or not to include. Certain products (including Crystal Reports (SQL Query Designer), Microsoft Access and the products (including Crystal Reports (SQL Query Designer), Microsoft Access and the very basic 'Visdata' demo that comes with Visual Basic) allow you to view this query very basic 'Visdata' demo that comes with Visual Basic) allow you to view this query as a native SQL statement. Be warned: Access puts lots of extra rubbish in the as a native SQL statement. Be warned: Access puts lots of extra rubbish in the statement that you don't need, such as square [] and curved () brackets; it also statement that you don't need, such as square [] and curved () brackets; it also prefixes fields with table names even if you are only using one. prefixes fields with table names even if you are only using one.

So the answer to 'How do I write advanced SQL queries without knowing any SQL?' So the answer to 'How do I write advanced SQL queries without knowing any SQL?' is to design your query in, say, MS-Access, click the View -> SQL View menu option, is to design your query in, say, MS-Access, click the View -> SQL View menu option, cut and paste into your own application, and hey-presto! You are an instant SQL cut and paste into your own application, and hey-presto! You are an instant SQL expert!expert!