8/10/2019 Schemas and Joins
1/59
SQL: Queries, Schemas, and Joins-OH MY!!!
Geoff Noel
8/10/2019 Schemas and Joins
2/59
You may ask yourself Well, How did I get
here. David Byrne Once in a lifetime
8/10/2019 Schemas and Joins
3/59
Databases come in all different shapes and sizes. They can be flat files of ASCIIdata (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 ofchanges to this data?
That is the job of the database management system, or DBMS. Some DBMSs arerelational. Those are RDBMS. The relational part refers to the fact that separatecollections of data within the reaches of the RDBMS can be looked at together inunison. 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 inline.
Databases Overview
8/10/2019 Schemas and Joins
4/59
What Is a Database?
What is a database and database management system. A database is a collection of related data. A database management system (DBMS) is a collection of programs that enablesusers to create and maintain a database.
Why use a database management system ?To control redundancy.To restrict unauthorized access.To provide persistent storage for program objects and data structures.To permit inference and actions using rules.To provide multiple user interfaces.To representing complex relationships among data.To enforce integrity constraints.To provide backup and recovery.
8/10/2019 Schemas and Joins
5/59
Database Client/Server Methodology
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 relationshipsbetween pieces of data. Codd's rules formed the basis for the developmentof systems to manage data. Today, Relational Database ManagementSystems (RDBMS) are the result of Codd's vision.
Data in an RDBMS are stored as rows of distinct information in tables. Astructured language is used to query (retrieve), store and change the data.The Structured Query Language (SQL) is an ANSI standard, and all majorcommercial RDBMS vendors provide mechanisms for issuing SQL
commands.
8/10/2019 Schemas and Joins
6/59
Single-Tier Database Design 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,typically a mainframe. The applications were simple but inefficient and did not work over LANs. Themodel did not scale the application and user interface code was tightly coupled to the databaselibraries.
The monolithic single-tier database design
8/10/2019 Schemas and Joins
7/59
The two-tier database design
Two-Tier Database Design
Two-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 frontend that accessed data through a connection ( socket ) to the back-end server. A two-tier database design,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 theappropriate results, and the client is responsible for the formatting and display of the data. Clients still use avendor-provided library of functions that manage the communication between client and server. Most of theselibraries are written in either the C language or Perl.Commercial database vendors realized the potential for adding intelligence to the database server. Theycreated proprietary techniques that allowed the database designer to develop macro programs for simpledata manipulation. These macros, called stored procedures, can cause problems relating to version controland maintenance. Because a stored procedure is an executable program living on the database, it is possiblefor the stored procedure to attempt to access named columns of a database table after the table has beenchanged.For example , if a column with the name id is changed to cust_id, the meaning of the original storedprocedure 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 thedata returned from a query are not expected. Again, this can be the result of the trigger reading a tablecolumn that has been altered.
8/10/2019 Schemas and Joins
8/59
The vendor-provided library limits them. Switching from one
database vendor to another requires a rewrite of a significantamount of code to the client application.
Version control is an issue. When the vendor updates the client-sidelibraries, the applications that utilize the database must berecompiled and redistributed.
Vendor libraries deal with low-level data manipulation. Typically,the base library only deals with queries and updates of single rowsor columns of data. This can be enhanced on the server side bycreating a stored procedure, but the complexity of the system thenincreases.
All of the intelligence associated with using and manipulating the data isimplemented in the client application, creating large client-side runtimes. Thisdrives up the cost of each client set.
Limitations of Two-Tier Database Design
8/10/2019 Schemas and Joins
9/59
Three-Tier Database DesignIn 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 multipleClient requests and manage the connection to one or more database servers. There does nothave to be just three tiers, but conceptually this is the next step.
Three-Tier Database Design
8/10/2019 Schemas and Joins
10/59
Advantages of Three-Tier Design It is multithreaded to manage multiple client connections simultaneously.
It can accept connections from clients over a variety of vendor-neutralprotocols (from HTTP to TCP/IP), then hand off the requests to theappropriate vendor-specific database servers, returning the replies to theappropriate clients.
It can be programmed with a set of "business rules" that manage the
manipulation of the data. Business rules could include anything fromrestricting access to certain portions of data to making sure that data isproperly formatted before being inserted or updated.
It prevents the client from becoming too heavy by centralizing process-intensive tasks and abstracting data representation to a higher level.It isolates the client application from the database system and frees acompany to switch database systems without having to rework the businessrules.It can asynchronously provide the client with the status of a current datatable or row.
8/10/2019 Schemas and Joins
11/59
Different RDBMS - History
DB2/UDB
NCR TeradataIngresInformixSybaseMySQLGupta/Centura -SQLbaseDBaseParadox . . . Many others
OtherDatabases
7.x
8.0.x8.1.x ( aka 8i)9.210G
Oracle
Sybase/ SQL Server 6.06.57.020002005
MS SQL Server
8/10/2019 Schemas and Joins
12/59
Various Methods of Connection
ODBC JDBCNativeOLE/ADOBCPSQL-NETSQL-LOADERHPL
8/10/2019 Schemas and Joins
13/59
Tools
I-SQLSQL WorksheetEnterprise Manager
ToadDB ArtisanQuery Analyzer
Win SQLO-SQLSQL-Plus
Enterprise ManagerDB ArtisanMS Query
AccessCrystalData DictionaryERD (EntityRelationship Diagram)
8/10/2019 Schemas and Joins
14/59
8/10/2019 Schemas and Joins
15/59
8/10/2019 Schemas and Joins
16/59
8/10/2019 Schemas and Joins
17/59
Schema By Object Type
S h B O
8/10/2019 Schemas and Joins
18/59
Schema By Owner
8/10/2019 Schemas and Joins
19/59
SQLbase SQL Talk utility
8/10/2019 Schemas and Joins
20/59
SQLbase SQL COnsole
8/10/2019 Schemas and Joins
21/59
8/10/2019 Schemas and Joins
22/59
8/10/2019 Schemas and Joins
23/59
8/10/2019 Schemas and Joins
24/59
What is a Schema ?
Pronounced -> skee-ma .
The structure of a database system, described in a formal language supported by
the database management system (DBMS). In a relational Database (RDBMS),the schema defines the tables, the fields in each table, and theRelationships between fields and tables. Schemas are generally stored in a datadictionary. Although a schema is defined in text database language, the term is
often used to refer to a graphical depiction of the database structure.
8/10/2019 Schemas and Joins
25/59
Database ObjectsDDL- data definition
TablesColumns
Data-typesIndexes
Primary keyForeign key
ViewsTable-spacesPartitionsConstraintsSynonyms
DML- data manipulationPackagesTriggersProceduresFunctions
ConstraintsSequences
8/10/2019 Schemas and Joins
26/59
Database actions
S Select .. > Queries U - Update
I - InsertD - Delete
8/10/2019 Schemas and Joins
27/59
Table Basics A relational database system
contains one or more objectscalled tables. The data orinformation for the database arestored in these tables. Tables areuniquely identified by their namesand are comprised of columns androws. Columns contain the columnname, data type, and any otherattributes for the column. Rowscontain the records or data for thecolumns. Here is a sample tablecalled "weather".
city, state, high, and low are thecolumns. The rows contain thedata 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
8/10/2019 Schemas and Joins
28/59
Primary Key (PK) A primary key is a tablecolumn that can be used touniquely identify every row ofthe table. A n y c o l u m n t h a thas th i s p roper ty wi l l do --these columns are calledcandidate keys. A table canhave many candidate keysbut only one primary key. Theprimary key cannot be null.
FooNumber FirstName LastName BarTab
21 Fred Jones 47
32 Bill Smith 23
87 Wendy Jones
32 Bob Stikino 943
8/10/2019 Schemas and Joins
29/59
Composite Primary Key A is a primary key consisting of more than one column. In the aboveexample, the combinations (RecordNo,FirstName), (RecordNo,Lastname),(RecordNo,FirstName,Lastname), and (FirstName,LastName) are allcandidate keys. Any combination including Age is not a candidate keybecause it contains a null.
Often, database designers add an extra column to their table designs, acolumn defined as an integer, which will hold a number. In Microsoft Access, this is an autonumber , in MySQL it's an auto-increment , in Oracleit's a sequence , and in SQL/Server it's an identity column. As these namessuggest, this integer is automatically assigned by the database, usuallyincrementally, sometimes using an initial value and increment that you canspecify. Some databases allow these numbers to be generated randomly.
The purpose of this type of automatically generated number is to act as thesurrogate primary key, usually in those situations similar to the abovewhere candidate keys are multi-column. The awkwardness of a multi-column candidate key becomes apparent as soon as you define a foreignkey on it.
8/10/2019 Schemas and Joins
30/59
A foreign key is a column, orcombination of columns, that containvalues that are found in the primary keyof some table (including, possibly, itself).
A foreign key may be null, and almostalways is not unique.That last statementmay be counterintuitive, so let's takeanother example. Here we have twotables that are related via a foreign key
As you can see, the ClrFK column in thesecond table is a foreign key to the ClrPKprimary key in the first table. Notice thatthe ClrPK values are unique and not null,but the ClrFK values may be null andoften repeat. A null foreign key meansthat tha t par t icular row does notparticipate in the relationship. The factthat many foreign key values repeatsimply reflects the fact that it's a one-to-many relationship.
In a one-to-many relationship, theprimary key has the "one" value, and theforeign key has the "many" values. Thetrick to remembering this is to keep inmind that the primary key must beunique.
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)
8/10/2019 Schemas and Joins
31/59
It Aint nothing its NULL
Null means either "don't know" or "notapplicable" -- it's not really the same as zero orany other default value, but more importantly,null is treated quite differently from other valuesin SQL, because it literally has no value.
Here's an example of a "don't know" null ->
As you can see, Fred's value is null, which youcould interpret as meaning that Fred didn't takethe test (maybe he has a medical exemption and
will take the test another day). It would be wrongto assign a zero, because that would beinterpreted as Fred having taken the test and notgetting a single answer right!
Student TestResult
Joe 87
Bill 73
Mary 56
Fred null
Sam 92
8/10/2019 Schemas and Joins
32/59
Now consider the following query
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 default
value 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 resourcesdatabase, where the value would be null for all active employees. To test for nulls, you can filter them out inthe WHERE clause
SELECT EmployeeID , (DateTerminated - DateHired) AS LengthOfService FROM EmployeeTableWHERE DateTerminated IS NOT NULL
which 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 nullresult.
Alternatively, you can use the COALESCE function to supply a non-null value
SELECT EmployeeID , ( COALESCE(DateTerminated,GETDATE()) - DateHired) AS LengthOfServiceFROM EmployeeTable where GETDATE()
returns 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 asabove, while for active employees, DateTerminated is null so COALESCE uses today's date instead.
8/10/2019 Schemas and Joins
33/59
What SQL?SQL or SEQUEL ?
SQL (pronounced "ess-que-el")
The acronym SQL stands for S tructured Query Language
SQL is used to communicate with a database. According to ANSI (American NationalStandards Institute), it is the standard language for relational database managementsystems. SQL statements are used to perform tasks such as update data on a database,or retrieve data from a database. Some common relational database managementsystems 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 additionalproprietary extensions that are usually only used on their system. However, the standardSQL commands such as "Select", "Insert", "Update", "Delete", "Create", and "Drop" canbe used to accomplish almost everything that one needs to do with a database.
Some people claim that this is a bad name, becauseSQL isn ' t (proper ly) s t ructuredi t ' s mo re than jus t q uer ies (e .g . inser t , upd ate , delete)i t i sn ' t a rea l comp ut ing lang uage (Very Debatable)
In any case, SQL is a database query language that was adopted as an industry standardin 1986. It has undergone two important revisions, SQL2 (also called SQL-92), and SQL3(also called SQL-99).
8/10/2019 Schemas and Joins
34/59
Selecting Data The select statement is used to query the
database and retrieve selected data thatmatch the criteria that you specify. Here isthe format of a simple select statement:
select "column1" [,"column2",etc] from"tablename" [where "condition"];
[ ] = optional The column names that follow the selectkeyword determine which columns will bereturned in the results. You can select asmany column names that you'd like, or youcan use a "*" to select all columns.
The table name that follows the keywordfrom specifies the table that will be queriedto retrieve the desired results.
The where clause (optional) specifies whichdata values or rows will be returned ordisplayed, based on the criteria describedafter the keyword where .Conditional selections used in the where clause:
= Equal
> Greater than
< Less than
>= Greater than or equal
8/10/2019 Schemas and Joins
35/59
select first, last, city from empinfo; select last,city, age from empinfo where age > 30;
select first, last, city, state from empinfo wherefirst LIKE 'J%';
select * from empinfo;
select first, last, from empinfo where last LIKE
'%s';
select first, last, age from empinfo where lastLIKE '%illia%';
select * from empinfo where first = 'Eric';
Sample Table: empinfo
first last id ag e city state
John Jones 9998 0 45 Payson Arizona
Mary Jones 9998
2 25 Payson Arizona
Eric Edwards 8823
2 32 San Diego Californi
a
MaryAnn
Edwards
88233 32 Phoenix Arizona
Ginger Howell 9800 2 42 Cottonwoo
d Arizona
Sebastian Smith
92001 23 Gila Bend Arizona
Gus Gray 2232 2 35 Bagdad Arizona
MaryAnn
May 3232 6 52 Tucson Arizona
Erica Williams 3232
7 60 Show Low Arizona
Leroy Brown 3238 0 22 Pinetop Arizona
Elroy Cleaver 3238 2 22 Globe Arizona
8/10/2019 Schemas and Joins
36/59
select first, last, city from empinfo; selectlast, city, age from empinfo where age >30;
Sample Table: empinfo
first last id ag e city state
John Jones 9998 0 45 Payson Arizona
Mary Jones 9998 2 25 Payson Arizona
Eric Edwards 8823
2 32 San Diego Californi
a
Mary Ann Edwards 8823
3 32 Phoenix Arizona
Ginger Howell 9800 2 42 Cottonwoo
d Arizona
Sebastian Smith
92001 23 Gila Bend Arizona
Gus Gray 2232 2 35 Bagdad Arizona
Mary Ann May 3232 6 52 Tucson Arizona
Erica Williams 3232 7 60 Show Low Arizona
Leroy Brown 3238 0 22 Pinetop Arizona
Elroy Cleaver 3238 2 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
8/10/2019 Schemas and Joins
37/59
select first, last, city, state from empinfo where first LIKE '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
8/10/2019 Schemas and Joins
38/59
select * from empinfo;
Sample Table: empinfo
first last id ag e 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
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
8/10/2019 Schemas and Joins
39/59
select first, last, from empinfo where lastLIKE '%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
8/10/2019 Schemas and Joins
40/59
select first, last, age from empinfo where lastLIKE '%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
8/10/2019 Schemas and Joins
41/59
select * from empinfo where first = '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
8/10/2019 Schemas and Joins
42/59
The LIKE pattern matching operator can also beused in the conditional selection of the whereclause. Like is a very powerful operator that allowsyou to select only rows that are "like" what youspecify. The percent sign "% can be used aswild card to match any possible characterthat might appear before or after the charactersspecified.
For example:
select first, last, city from empinfo whereFirst LIKE 'Er%';
This SQL statement will match any first namesthat start with 'Er'.
Strings must be in single quotes.
Or you can specify,select first, last from empinfo where last
LIKE '%s';
This statement will match any last names that endin a 's'.
select * from empinfo where first = 'Eric';
This will only select rows where the first nameequals '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
8/10/2019 Schemas and Joins
43/59
The 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 Cus tomer table and aTransact ion table, they both must contain a common element, such asCustomerID 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 fullyqualified column names. This is a dot notation that combines the names oftables and columns. For example, if I have two tables, Cus tomer and Transact ion , and they both contain the column CustomerID , Id use the dot notation, as inCustomer.CustomerID and Transaction.CustomerID, to let the database knowwhich column from which table Im referring.
Now that weve examined the basic theory, lets take a look at the various types of joins and examples of each.
The basic JOIN statement
8/10/2019 Schemas and Joins
44/59
The basic JOIN statement
A basic JOIN statement has the following format:
SELECT Customer.CustomerID, TransID, TransAmt FROM Customer JOIN TransactionON Customer.CustomerID = Transaction.CustomerID;
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 thestatement:
SELECT Customer.CustomerID, TransID, TransAmt FROM Customer, Transaction;
However, the example is useful to point out a few noteworthy concepts:
TransID and TransAmt do not require fully qualified names because they exist in only one of thetables. You can use fully qualified names for readability if you wish.The Customer table is considered to be the left table because it was called first. Likewise, theTransaction table is the right table.You can use more than two tables, in which case each one is naturally joined to the cumulativeresult in the order they are listed, unless controlled by other functionality such as join hints orparenthesis.You may use WHERE and ORDER BY clauses with any JOIN statement to limit the scope of yourresults. 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 denotethe end 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 acomplicated concept, you will see that its a powerful timesaving resource thats relatively easy to understand. Use this functionality to get related information from multiple tables with a single queryand to skillfully reference normalized data. Once youve mastered JOINs, you can elegantly maneuverwithin even the most complex database .
8/10/2019 Schemas and Joins
45/59
Inner join
In relational databases, a join operation matches recordsin two tables. The two tables must be joined by at leastone common field. That is, the join fieldis a member ofboth tables. Typically, a join operation is part of aSELECT query .
select * from A, B where A.x = B.y
The column names (x and y in this example) are often,but not necessarily, the same.
http://www.webopedia.com/TERM/j/relational_database.htmlhttp://www.webopedia.com/TERM/j/field.htmlhttp://www.webopedia.com/TERM/j/query.htmlhttp://www.webopedia.com/TERM/j/query.htmlhttp://www.webopedia.com/TERM/j/field.htmlhttp://www.webopedia.com/TERM/j/relational_database.html8/10/2019 Schemas and Joins
46/59
Outer Join(database)outer join - A less commonly used variant of the inner join relationaldatabase operation. An inner join selects rows from two tables such that the
value in one column of the first table also appears in a certain column of thesecond table. For an outer join, the result also includes all rows from the firstoperand ("left outer join", "*="), or the second operand ("right outer join","=*"), or both ("full outer join", "*=*"). A field in a result row will be null ifthe corresponding input table did not contain a matching row.
For example, if we want to list all employees and their employee number, butnot all employees have a number, then we could say (in SQL ):
SELECT employee.name, empnum.number WHERE employee.id *=empnum.id
The "*=" means "left outer join" and means that all rows from the"employee" table will appear in the result, even if there is no match for their IDin the empnum table.
http://computing-dictionary.thefreedictionary.com/inner%20joinhttp://computing-dictionary.thefreedictionary.com/relational%20databasehttp://computing-dictionary.thefreedictionary.com/relational%20databasehttp://computing-dictionary.thefreedictionary.com/SQLhttp://computing-dictionary.thefreedictionary.com/SQLhttp://computing-dictionary.thefreedictionary.com/relational%20databasehttp://computing-dictionary.thefreedictionary.com/relational%20databasehttp://computing-dictionary.thefreedictionary.com/inner%20join8/10/2019 Schemas and Joins
47/59
The notorious CROSS JOIN
The CROSS JOIN has earned a bad reputation because its very resourceintensive 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 fromeach table. Take the following example:
SELECT CustomerName, TransDate, TransAmt FROM Customer CROSS JOIN Transaction;
With the CROSS JOIN, you arent actually free to limit the results, but you can use the ORDER BYclause to control the way they are returned. If the tables joined in this example contained only fiverows each, you would get 25 rows of results. Every CustomerName would be listed as associated withevery TransDate and TransAmt.
I really did try to come up with examples where this function was useful, and they were all verycontrived. However, Im sure someone out there is generating lists of all their products in all possiblecolors or something similar, or we wouldnt have this wonderful but dangerous feature.
8/10/2019 Schemas and Joins
48/59
The INNER JOIN drops rows When you perform an INNER JOIN, only rows that match up are returned. Anytime a row from either table doesnt have corresponding values from the othertable, it is disregarded. Because stray rows arent included, you dont have any ofthe left and right nonsense to deal with and the order in which you presenttables matters only if you have more than two to compare. Since this is a simpleconcept, heres a simple example:
SELECT CustomerName, TransDate FROM Customer INNER JOIN Transaction ON Customer.CustomerID = Transaction.CustomerID;
If a row in the Transaction table contains a CustomerID thats not listed in theCustomer table, that row will not be returned as part of the result set. Likewise, if
the Customer table has a CustomerID with no corresponding rows in the Transaction table, therow from the Customer table wont be returned.
8/10/2019 Schemas and Joins
49/59
The OUTER JOIN can include mismatched rows OUTER JOINs, sometimes called complex joins, arent actually complicated. They are so -calledbecause 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 INNERJOIN would have dropped. Which rows are included depends on the type of OUTER JOIN that is usedand the order the tables were presented.
There are three types of an OUTER JOIN: LEFT, RIGHT, and FULL. As youve probably guessed, the LEFT OUTER JOIN keeps the stray rows from the left table (the one listed first in your querystatement). In the result set, columns from the other table that have no corresponding data are filledwith NULL values. Similarly, the RIGHT OUTER JOIN keeps stray rows from the right table, fillingcolumns from the left table with NULL values. The FULL OUTER JOIN keeps all stray rows as part ofthe result set. Here is your example:
SELECT CustomerName, TransDate, TransAmt FROM Customer LEFT OUTER JOIN Transaction ONCustomer.CustomerID = Transaction.CustomerID;
Customer names that have no associated transactions will still be displayed. However, transactionswith no corresponding customers will not, because we used a LEFT OUTER JOIN and the Customer table was listed first.
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 OUTERJOIN, and FULL OUTER JOIN, respectively.
SQL S bq
8/10/2019 Schemas and Joins
50/59
SQL Subquery
It is possible to embed a SQL statement within another. Whenthis is done on the WHERE or the HAVING statements, wehave a subquery construct. What is subquery useful for? First, itcan also be used to join tables. Also, there are cases where theonly way to correlate two tables is through a subquery.
The syntax is as follows:SELECT "column_name1"FROM "table_name"
WHERE "column_name2" [Comparison Operator](SELECT "column_name1"FROM "table_name"
WHERE [Condition])
[Comparison Operator] could be equality operators such as =, >,=,
8/10/2019 Schemas and Joins
51/59
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 GeographyWHERE region_name = 'West')
SUM(Sales)
2050
In this example, instead of joining the two tables directly and then adding up only the salesamount for stores in the West region, we first use the subquery to find out which storesare in the West region, and then we sum up the sales amount for these stores.
8/10/2019 Schemas and Joins
52/59
select 'ALTER '||OBJECT_TYPE|| '||OWNER||'.'||OBJECT_NAME||' compile;' fromdba_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 Services
8/10/2019 Schemas and Joins
53/59
Originally not to be available until the Yukon release of SQL Server, Microsoft decided to releaseReporting 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 buildbusiness intelligence and reporting solutions. Until now, developers were required to embedreports into their applications, or organizations were required to purchase expensive and
sometimes problematic third-party reporting solutions. Now, Reporting Services offer a completesolution for distributing reports across the enterprise; enabling businesses to make decisionsbetter and faster.
Overview of Reporting Services Reporting Services is a scalable, secure, robust reporting solution for SQL Server. It supports thecomplete reporting lifecycle by including tools for report creation, execution, distribution, andmanagement. New users can have Reporting Services installed and new reports published withina matter of hours instead of days or weeks.
Reporting Services consists of the following key components:
Report Designer : Supports the report creation phase of the report lifecycle. It is an add-on tool forany edition of Visual Studio .NET 2003, suitable for both programmers and non-programmers.Report Server : Provides services for execution and distribution of reports.Report Manager : A Web-based administration tool for managing the Report Server. Report Designer Report Designer is a Visual Studio .NET 2003 add-on and is included with Reporting Services(see Figure A ). As the name implies, it provides developers and non-developers an intuitive toolto create sophisticated reports. Users get standard reporting functionality such as grouping,sorting, and report formatting. This should be sufficient for most reporting needs. For moreadvanced reports, the Report Designer has full VB.NET support. Plus, designers can add ActiveXcontrols to their reports to create rich, live, interactive reports.
8/10/2019 Schemas and Joins
54/59
One of the more compelling features of Report Designer is the ability to have dynamic query-
8/10/2019 Schemas and Joins
55/59
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 thereports (i.e., department names, office locations, employee names, etc.). You simply have tocreate a new dataset 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 isan 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 productofferings from independent software vendors (ISVs) today.
Though Reporting Services requires SQL Server as its repository, Report Designer can connect toall types of data sources including OLE DB, ODBC, Oracle, SQL Server, and others. It also hasmany rendering options such as HTML, Microsoft Excel, PDF, CSV, XML, and others. The list canalso be extended by third parties or by using the Reporting Services extension library.
Report Server The Report Server provides the repository, management, execution, and delivery functions. It isscalable and secure, and can support the most demanding reporting needs. It consists of severalsubcomponents, including:
Request Handler : Handles all inbound server requests and routes them to the appropriatecomponent.Scheduling and Delivery Processor : Provides the scheduling and delivering functionality, and itcan be extended to deliver reports to other devices such as fax machines or printers.Report Processor : Provides the execution functionality and it, too, can be extended to render tonew output formats such as a Microsoft Word document.Report Server Database : All the data required by Reporting Services is stored in the ReportServer Database, which must be a SQL Server. This includes everything from server settings toreport definitions, even to cached data from a report execution. Report Manager Report Manager provides administrators an easy-to-use tool for configuring the server andmanaging the reports. Using Report Manager, administrators can configure security, changeserver settings, schedule reports for execution, and maintain the structure of the report folders
(see Figure B ).
8/10/2019 Schemas and Joins
56/59
Administrators have a variety of options for executing reports including on-demand cached reporting
8/10/2019 Schemas and Joins
57/59
Administrators 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 areconfigurable at the report level through Report Manager.
Report Manager supports both push and pull distribution options. For e-mail delivery, Report Managercan include a link to the report, attach the report to the message, or embed reports directly into themessage using Web archive. This eliminates one extra step for the reader of the report.
Where you need Reporting Services
Here are a few typical scenarios where Reporting Services will be invaluable:
New application development :Most applications have reporting requirements that are sometimes very complex. With Reportingservices, the business analyst, who usually has a better understanding of what is required, is able tofulfill these requirements. This frees a developer to perform other development tasks and not developtedious, time-consuming reports. Rarely do I meet a developer who enjoys writing reports.Furthermore, these reports will be easier to maintain and support.
Existing applications :
Because of the complexity and time required to embed reports into applications, reports are oftencreated 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 completereporting solution, embedded within the application.
8/10/2019 Schemas and Joins
58/59
Executive dashboardExecutive dashboard is the buzzword for providing executives a comprehensive view of their business,commonly in an Enterprise Portal. Reporting Services includes several key features for an executivedashboard, such as My Reports, My Subscriptions, push/pull delivery, numerous rendering options,
and support for Web services.
Though very powerful, there are a couple of scenarios that would not be suitable for ReportingServices:
Applications using third-party reporting solutions :Reporting Services supply only a migration tool
for Microsoft Access. If you have a significant investment in another tool and it supports your needs,youll need to carefully consider the costs of migrating to Reporting Services.
Enterprise reporting solution :Though non-programmers can be very productive with Report Designer, there are reporting toolsabsent from Report Designer that experienced report creators will miss. For example, ReportingServices does not include a database abstraction layer to hide database details from users. Thisabstraction is very useful if you want to deploy a reporting solution to a large, semi-technical audience.
8/10/2019 Schemas and Joins
59/59
Dirty Little Secret SHHH!! 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 anydeveloper. For a beginner it is yet another skill you need to learn, but there is ashortcut that will allow you to write SQL without knowing SQL, and as a result ofusing these shortcuts you will actually learn to write native SQL. So what are theseshortcuts?, I hear you cry, well, read on and find out.
All good database and report writing applications allow you to create database
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. Certainproducts (including Crystal Reports (SQL Query Designer), Microsoft Access and thevery basic 'Visdata' demo that comes with Visual Basic) allow you to view this queryas a native SQL statement. Be warned: Access puts lots of extra rubbish in thestatement that you don't need, such as square [] and curved () brackets; it alsoprefixes 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?'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 SQLexpert!
Top Related