SlideShare a Scribd company logo
Introduction To TSQL Unit 1 Developed by Michael Hotek
Definitions SQL - Structured Query Language SQL is probably one of the simplest languages you will ever learn.  It is also very simple to underestimate.  DON’T!!!  This is arguably the most powerful language you will learn. SQL is a set oriented language.  It was designed and built to manage groups of data. ER Diagram - Entity Relationship diagram An ER Diagram, also known as a database schema, gives you a graphical depiction of the database you are working with.
PUBS Database
Unit 1 Goals What is a database What is a table Rows and columns Connecting to your database Change databases Overview of PUBS database Simple select Select all columns from a table Select specific columns from a table Concatenate two columns Create a query to give formatted output
Databases At the most basic level a database is really just a file. Databases come in all shapes and sizes.  Some are large and some are small.  But each database generally serves a particular purpose. Examples:  Tracking employee payroll, sales data on a particular sales line, stock data for a particular industry All databases are made up of objects.  The most important object (and the one we will learn how to use in this class) is a table.
Tables A table is a storage structure made up of rows and columns.  (Sort of like a spreadsheet.) Due to the differing terminologies, there are interchangeable sets of terms: Database Mathematical Data Processing Table Relation File Row Tuple Record Column Attribute Field These terms are used interchangeably, but we will generally use the table – row – column terminology
Tables  cont. You will also hear a table referred to as an entity.  (Hence the name Entity – Relationship Diagram) In the most basic sense, an entity is a person, place, thing, or idea. Entities usually become tables Example:  books, publishers, titles, authors
Connect to a database In this class we will use a tool called ISQL/W.  This stands for Interactive SQL / Windows.  This is where we will execute all of our queries from.  A query as the term implies is a question we ask the database. In other environments you will see this query tool called by different names.  It is generally referred to as just isql. Regardless of name, they all perform the same purpose.  This is to give you an interface for sending SQL statements and receiving results.
SQL Server Login Startup ISQL/W Login window A SQL Server can have many different databases running on it at the same time.  Database setup on the server  Assign databases Your first SQL statement use <database> This tells the SQL Server what database you will be using to perform your queries. Example:  use PUBS1
Verify your database To check the database you are accessing use: select db_name() Throughout this course some of the things we discuss will be MS SQL Server specific. The DBMS you are using should have a command or function similar to this, but not necessarily the same.
Basic syntax rules SQL keywords (use, select, from, where, etc.) are case insensitive. select is the same as SELECT is the same as SeLeCt, etc.  However depending on the DBMS (Database Management System), the columns might be case sensitive. select title_id from titles is not necessarily the same as SELECT TITLE_ID FROM TITLES The databases we have setup on our server are case insensitive.
Rules  cont. Spacing does not matter (for the most part).  select title_id… is the same as select  title_id… However, you must still separate words.  You can not use the following: selecttitle_id…  This will give a syntax error, because SQL Server must be able to find your SQL keywords.
Rules  cont. Carriage returns are ignored select title_id from titles is the same as select title_id from titles The spacing and carriage returns just make reading your SQL a lot easier. The general format used by most people is to place the separate clauses of the statement on different lines
PUBS Database PUBS is a database for a fictitious book distributor that sells books to book resellers.  This is the database for which you have an ER diagram for. ER diagram explanation You will generally get an ER diagram at each client when you begin work on a project.  If you don’t have one, ask for one.  This will save time in trying to determine what data is where and how everything is linked together.  If you can’t get one, don’t panic!  There are ways to get the database to tell you what it contains.
Select SELECT Statement  Retrieves rows from the database. SELECT [ALL | DISTINCT] <select_list>  INTO [<new_table_name>]  [FROM <table_name> [, <table_name2> [..., <table_name16>]] [WHERE <clause>]  [GROUP BY <clause>]  [HAVING <clause>]  [ORDER BY <clause>] [COMPUTE <clause>]  [FOR BROWSE] where  <table_name> | <view_name> = [[<database>.]<owner>.]{<table_name>. | <view_name>.} <joined_table> = {<table_name> CROSS JOIN <table_name> | <table_name> {INNER | LEFT [OUTER] | RIGHT [OUTER] | FULL [OUTER]}  JOIN <table_name> ON <search_conditions>} <optimizer_hints> One or more of the following, separated with a space: [INDEX = {<index_name> | <index_id>}] [NOLOCK]  [HOLDLOCK]  [UPDLOCK]  [TABLOCK]  [PAGLOCK]  [TABLOCKX] [FASTFIRSTROW] WHERE <clause> = WHERE <search_conditions> GROUP BY <clause> = GROUP BY [ALL] <aggregate_free_expression>  [[, <aggregate_free_expression>]...] [WITH {CUBE | ROLLUP}] HAVING <clause> = HAVING <search_conditions> ORDER BY <clause> = ORDER BY {{<table_name>. | <view_name>.}<column_name> | <select_list_number> | <expression>} [ASC | DESC]  [...{{<table_name16>. | <view_name16>.}<column_name> | <select_list_number> | <expression>} [ASC | DESC]] COMPUTE <clause> = COMPUTE <row_aggregate>(<column_name>) [, <row_aggregate>(<column_name>)...] [BY <column_name> [, <column_name>]...]
Select A select statement is used to retrieve data from a database.  As you can see from the syntax above, a select statement can get very complicated. Depending on the type of SQL statement you are using most of this is optional.
Select In order to get information from the database, you must tell the database what you are looking for.  The first step along this journey is to get some simple information from the database. select 'Mary had a little lamb.' -------------- Mary had a little lamb. (1 row affected) If you ever want to return a specific phrase from a database, use this construct.
Select An asterisk (*) is used to designate all columns in a table. select * We also need to tell it which table to get the data  from . select * from authors The main sections in every SQL statement are called clauses.  The three clauses will will focus on are the select, from, and where.
Select select * from authors au_id  au_lname  au_fname  phone ...  ----------- -------------------- -------------------- ------------  172-32-1176 White  Johnson  408 496-7223... 213-46-8915 Green  Marjorie  415 986-7020... 238-95-7766 Carson  Cheryl  415 548-7723... 267-41-2394 O'Leary  Michael  408 286-2428... 274-80-9391 Straight  Dean  415 834-2919... 341-22-1782 Smith  Meander  913 843-0462... 409-56-7008 Bennet  Abraham  415 658-9932... 427-17-2319 Dull  Ann  415 836-7128... 472-27-2349 Gringlesby  Burt  707 938-6445... 486-29-1786 Locksley  Charlene  415 585-4620... 527-72-3246 Greene  Morningstar  615 297-2723... 648-92-1872 Blotchet-Halls  Reginald  503 745-6402... 672-71-3249 Yokomoto  Akiko  415 935-4228... 712-45-1867 del Castillo  Innes  615 996-8275... 722-51-5454 DeFrance  Michel  219 547-9982... 724-08-9931 Stringer  Dirk  415 843-2991... 724-80-9391 MacFeather  Stearns  415 354-7128... 756-30-7391 Karsen  Livia  415 534-9219... 807-91-6654 Panteley  Sylvia  301 946-8853... 846-92-7186 Hunter  Sheryl  415 836-7128... 893-72-1158 McBadden  Heather  707 448-4982... 899-46-2035 Ringer  Anne  801 826-0752... 998-72-3567 Ringer  Albert  801 826-0752... (23 row(s) affected)
Select We can limit the columns returned by specifying them instead of using *. select au_lname, au_fname from authors au_lname  au_fname  ---------------------------------------- --------------------  White  Johnson  Green  Marjorie  Carson  Cheryl  O'Leary  Michael  Straight  Dean  Smith  Meander  Bennet  Abraham  Dull  Ann  Gringlesby  Burt  Locksley  Charlene  Greene  Morningstar  Blotchet-Halls  Reginald  Yokomoto  Akiko  del Castillo  Innes  DeFrance  Michel  Stringer  Dirk  MacFeather  Stearns  Karsen  Livia  Panteley  Sylvia  Hunter  Sheryl  McBadden  Heather  Ringer  Anne  Ringer  Albert  (23 row(s) affected)
Select When you specify columns, you do not have to specify them in the order they appear in the table. You could have also executed the following: select au_fname, au_lname from authors   au_fname  au_lname  -------------------- ----------------------------------------  Johnson  White  Marjorie  Green  Cheryl  Carson  Michael  O'Leary  Dean  Straight  Meander  Smith  Abraham  Bennet  Ann  Dull  Burt  Gringlesby  ... (23 row(s) affected)
Concatenation We can also combine data together.  This is called concatenation. We really want to display the first name and the last name separated by a space and then the rest of the data.  The plus symbol (+) is the most widely used symbol for concatenation.  (A double pipe || is sometimes used, but very rarely.) select au_fname+au_lname from authors JohnsonWhite  MarjorieGreen  CherylCarson  MichaelO'Leary  DeanStraight  MeanderSmith  AbrahamBennet  AnnDull  BurtGringlesby  CharleneLocksley  MorningstarGreene  ReginaldBlotchet-Halls  AkikoYokomoto  Innesdel Castillo  ... (23 row(s) affected)
Concatenation  cont. Concatenation is used for string (character) data.  If a concatenation operator (+) is used on numeric data, the data is simply added together. select title_id,price,advance from titles title_id price  advance  -------- -------------------------- --------------------------  BU1032  19.99  5,000.00  BU1111  11.95  5,000.00  BU2075  2.99  10,125.00  BU7832  19.99  5,000.00  MC2222  19.99  0.00  ... (18 row(s) affected) select title_id,price+advance from titles title_id  -------- --------------------------  BU1032  5,019.99  BU1111  5,011.95  BU2075  10,127.99  BU7832  5,019.99  MC2222  19.99  ... (18 row(s) affected)
Select By combining the first select (selecting a constant) with the select on authors we did above, we can get some formatted output from the database. select au_fname+’ ‘ + au_lname, city + ’,’ + state + ’ ‘+zip from authors  -------------------------------------- -----------------------------  Johnson White  Menlo Park,CA 94025  Marjorie Green  Oakland,CA 94618  Cheryl Carson  Berkeley,CA 94705  Michael O'Leary  San Jose,CA 95128  Dean Straight  Oakland,CA 94609  Meander Smith  Lawrence,KS 66044  Abraham Bennet  Berkeley,CA 94705  Ann Dull  Palo Alto,CA 94301  Burt Gringlesby  Covelo,CA 95428  ... (23 row(s) affected) One of the many things you should take away from this class is the ability to put together a SQL statement like the one above.  This simple principle saves DBAs hundreds of hours and year and makes their jobs much more simple.
Aliases The title for our previous result set isn't too informative, and we really don't want to display our formula. We can rename or alias a column in two ways Use a space and then the alias Specify the keyword as and then the alias select au_fname+’ ‘ + au_lname, city + ’,’ + state + ’ ‘+zip  Name_Address from authors select au_fname+’ ‘ + au_lname, city + ’,’ + state + ’ ‘+zip as Name_Address from authors
Aliases We can apply aliases in two places within our SQL statements Select clause From clause By specifying an alias in the select clause we can rename the column headers for the output By specifying an alias in the from clause, we can save some typing and also perform some higher level queries which will require this.  (This will be demonstrated in subsequent units.)
Unit 1 Review A database is a collection of objects, the most prominent of which is a table. A table consists of rows/tuples/records and columns/attributes/fields. use <dbname> allows you to select a database SQL keywords are not case sensitive. Spacing and carriage returns are not needed. You can include a constant in your result set by hadding it just as you would a column An * allows you to select all columns in a table A + is used for concatenating two strings.
Unit 1 Exercises Time allotted for exercises is 30 minutes

More Related Content

PPT
Intro To TSQL - Unit 4
PPT
Intro To TSQL - Unit 3
PPT
Intro To TSQL - Unit 5
PPT
Intro To TSQL - Unit 2
PPT
Intro to tsql unit 4
PPT
Physical elements of data
PPT
Intro to tsql unit 3
PDF
Intro To TSQL - Unit 4
Intro To TSQL - Unit 3
Intro To TSQL - Unit 5
Intro To TSQL - Unit 2
Intro to tsql unit 4
Physical elements of data
Intro to tsql unit 3

What's hot (17)

PPT
Database Tables and Data Types
PPTX
introdution to SQL and SQL functions
PPTX
Sql fundamentals
PPTX
SQL Server Learning Drive
PPT
Optimizing Data Accessin Sq Lserver2005
DOC
A must Sql notes for beginners
PPTX
SQL Basics
PPT
MS SQL Server 1
PPTX
Intro to t sql – 3rd session
PPT
Introduction to-sql
PPTX
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
PPT
Ms sql server ii
PPT
SQL : introduction
PDF
Chapter 4 Structured Query Language
PPTX
Introduction to SQL
PDF
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
PPTX
Introduction to SQL
Database Tables and Data Types
introdution to SQL and SQL functions
Sql fundamentals
SQL Server Learning Drive
Optimizing Data Accessin Sq Lserver2005
A must Sql notes for beginners
SQL Basics
MS SQL Server 1
Intro to t sql – 3rd session
Introduction to-sql
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Ms sql server ii
SQL : introduction
Chapter 4 Structured Query Language
Introduction to SQL
Sql notes, sql server,sql queries,introduction of SQL, Beginner in SQL
Introduction to SQL
Ad

Viewers also liked (9)

TXT
PPTX
TSQL Advanced Query Techniques
PPT
7a advanced tsql
PPTX
SQL Server Reporting Services 2008
PPT
Performance Tuning And Optimization Microsoft SQL Database
PPTX
Sql server basics
PPTX
Windowforms controls c#
PPS
Oracle Database Overview
TSQL Advanced Query Techniques
7a advanced tsql
SQL Server Reporting Services 2008
Performance Tuning And Optimization Microsoft SQL Database
Sql server basics
Windowforms controls c#
Oracle Database Overview
Ad

Similar to Intro To TSQL - Unit 1 (20)

PPT
Intro to tsql unit 1
PPTX
Intro to T-SQL – 2nd session
PPT
Sql 2006
PDF
Basic sqlstatements
PDF
Basic sqlstatements
DOCX
DOCX
Learning sql from w3schools
DOC
Introduction to sql
PDF
COIS 420 - Practice01
PPTX
lect 2.pptx
PPTX
PPTX
Sql slid
DOCX
Sql intro
DOC
DOC
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
PPT
The Basic of Standard Query Language Statements.ppt
PPTX
SQL command practical power point slides, which help you in learning sql.pptx
PPTX
SQL command practical power point slides, which help you in learning sql.pptx
PPT
Basic sql statements
PPT
Db1 lecture4
Intro to tsql unit 1
Intro to T-SQL – 2nd session
Sql 2006
Basic sqlstatements
Basic sqlstatements
Learning sql from w3schools
Introduction to sql
COIS 420 - Practice01
lect 2.pptx
Sql slid
Sql intro
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
The Basic of Standard Query Language Statements.ppt
SQL command practical power point slides, which help you in learning sql.pptx
SQL command practical power point slides, which help you in learning sql.pptx
Basic sql statements
Db1 lecture4

Recently uploaded (20)

PDF
How to Get Funding for Your Trucking Business
PPTX
Principles of Marketing, Industrial, Consumers,
PPT
340036916-American-Literature-Literary-Period-Overview.ppt
DOCX
Euro SEO Services 1st 3 General Updates.docx
PDF
Ôn tập tiếng anh trong kinh doanh nâng cao
PDF
A Brief Introduction About Julia Allison
PDF
Roadmap Map-digital Banking feature MB,IB,AB
PDF
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
PDF
Training And Development of Employee .pdf
PPTX
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
PDF
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
PDF
Chapter 5_Foreign Exchange Market in .pdf
PPTX
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
DOCX
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
PDF
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
PPT
Chapter four Project-Preparation material
PPTX
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
PDF
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry
PDF
Laughter Yoga Basic Learning Workshop Manual
How to Get Funding for Your Trucking Business
Principles of Marketing, Industrial, Consumers,
340036916-American-Literature-Literary-Period-Overview.ppt
Euro SEO Services 1st 3 General Updates.docx
Ôn tập tiếng anh trong kinh doanh nâng cao
A Brief Introduction About Julia Allison
Roadmap Map-digital Banking feature MB,IB,AB
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
Training And Development of Employee .pdf
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
Chapter 5_Foreign Exchange Market in .pdf
AI-assistance in Knowledge Collection and Curation supporting Safe and Sustai...
unit 2 cost accounting- Tender and Quotation & Reconciliation Statement
pdfcoffee.com-opt-b1plus-sb-answers.pdfvi
Chapter four Project-Preparation material
The Marketing Journey - Tracey Phillips - Marketing Matters 7-2025.pptx
Katrina Stoneking: Shaking Up the Alcohol Beverage Industry
Laughter Yoga Basic Learning Workshop Manual

Intro To TSQL - Unit 1

  • 1. Introduction To TSQL Unit 1 Developed by Michael Hotek
  • 2. Definitions SQL - Structured Query Language SQL is probably one of the simplest languages you will ever learn. It is also very simple to underestimate. DON’T!!! This is arguably the most powerful language you will learn. SQL is a set oriented language. It was designed and built to manage groups of data. ER Diagram - Entity Relationship diagram An ER Diagram, also known as a database schema, gives you a graphical depiction of the database you are working with.
  • 4. Unit 1 Goals What is a database What is a table Rows and columns Connecting to your database Change databases Overview of PUBS database Simple select Select all columns from a table Select specific columns from a table Concatenate two columns Create a query to give formatted output
  • 5. Databases At the most basic level a database is really just a file. Databases come in all shapes and sizes. Some are large and some are small. But each database generally serves a particular purpose. Examples: Tracking employee payroll, sales data on a particular sales line, stock data for a particular industry All databases are made up of objects. The most important object (and the one we will learn how to use in this class) is a table.
  • 6. Tables A table is a storage structure made up of rows and columns. (Sort of like a spreadsheet.) Due to the differing terminologies, there are interchangeable sets of terms: Database Mathematical Data Processing Table Relation File Row Tuple Record Column Attribute Field These terms are used interchangeably, but we will generally use the table – row – column terminology
  • 7. Tables cont. You will also hear a table referred to as an entity. (Hence the name Entity – Relationship Diagram) In the most basic sense, an entity is a person, place, thing, or idea. Entities usually become tables Example: books, publishers, titles, authors
  • 8. Connect to a database In this class we will use a tool called ISQL/W. This stands for Interactive SQL / Windows. This is where we will execute all of our queries from. A query as the term implies is a question we ask the database. In other environments you will see this query tool called by different names. It is generally referred to as just isql. Regardless of name, they all perform the same purpose. This is to give you an interface for sending SQL statements and receiving results.
  • 9. SQL Server Login Startup ISQL/W Login window A SQL Server can have many different databases running on it at the same time. Database setup on the server Assign databases Your first SQL statement use <database> This tells the SQL Server what database you will be using to perform your queries. Example: use PUBS1
  • 10. Verify your database To check the database you are accessing use: select db_name() Throughout this course some of the things we discuss will be MS SQL Server specific. The DBMS you are using should have a command or function similar to this, but not necessarily the same.
  • 11. Basic syntax rules SQL keywords (use, select, from, where, etc.) are case insensitive. select is the same as SELECT is the same as SeLeCt, etc. However depending on the DBMS (Database Management System), the columns might be case sensitive. select title_id from titles is not necessarily the same as SELECT TITLE_ID FROM TITLES The databases we have setup on our server are case insensitive.
  • 12. Rules cont. Spacing does not matter (for the most part). select title_id… is the same as select title_id… However, you must still separate words. You can not use the following: selecttitle_id… This will give a syntax error, because SQL Server must be able to find your SQL keywords.
  • 13. Rules cont. Carriage returns are ignored select title_id from titles is the same as select title_id from titles The spacing and carriage returns just make reading your SQL a lot easier. The general format used by most people is to place the separate clauses of the statement on different lines
  • 14. PUBS Database PUBS is a database for a fictitious book distributor that sells books to book resellers. This is the database for which you have an ER diagram for. ER diagram explanation You will generally get an ER diagram at each client when you begin work on a project. If you don’t have one, ask for one. This will save time in trying to determine what data is where and how everything is linked together. If you can’t get one, don’t panic! There are ways to get the database to tell you what it contains.
  • 15. Select SELECT Statement Retrieves rows from the database. SELECT [ALL | DISTINCT] <select_list> INTO [<new_table_name>] [FROM <table_name> [, <table_name2> [..., <table_name16>]] [WHERE <clause>] [GROUP BY <clause>] [HAVING <clause>] [ORDER BY <clause>] [COMPUTE <clause>] [FOR BROWSE] where <table_name> | <view_name> = [[<database>.]<owner>.]{<table_name>. | <view_name>.} <joined_table> = {<table_name> CROSS JOIN <table_name> | <table_name> {INNER | LEFT [OUTER] | RIGHT [OUTER] | FULL [OUTER]} JOIN <table_name> ON <search_conditions>} <optimizer_hints> One or more of the following, separated with a space: [INDEX = {<index_name> | <index_id>}] [NOLOCK] [HOLDLOCK] [UPDLOCK] [TABLOCK] [PAGLOCK] [TABLOCKX] [FASTFIRSTROW] WHERE <clause> = WHERE <search_conditions> GROUP BY <clause> = GROUP BY [ALL] <aggregate_free_expression> [[, <aggregate_free_expression>]...] [WITH {CUBE | ROLLUP}] HAVING <clause> = HAVING <search_conditions> ORDER BY <clause> = ORDER BY {{<table_name>. | <view_name>.}<column_name> | <select_list_number> | <expression>} [ASC | DESC] [...{{<table_name16>. | <view_name16>.}<column_name> | <select_list_number> | <expression>} [ASC | DESC]] COMPUTE <clause> = COMPUTE <row_aggregate>(<column_name>) [, <row_aggregate>(<column_name>)...] [BY <column_name> [, <column_name>]...]
  • 16. Select A select statement is used to retrieve data from a database. As you can see from the syntax above, a select statement can get very complicated. Depending on the type of SQL statement you are using most of this is optional.
  • 17. Select In order to get information from the database, you must tell the database what you are looking for. The first step along this journey is to get some simple information from the database. select 'Mary had a little lamb.' -------------- Mary had a little lamb. (1 row affected) If you ever want to return a specific phrase from a database, use this construct.
  • 18. Select An asterisk (*) is used to designate all columns in a table. select * We also need to tell it which table to get the data from . select * from authors The main sections in every SQL statement are called clauses. The three clauses will will focus on are the select, from, and where.
  • 19. Select select * from authors au_id au_lname au_fname phone ... ----------- -------------------- -------------------- ------------ 172-32-1176 White Johnson 408 496-7223... 213-46-8915 Green Marjorie 415 986-7020... 238-95-7766 Carson Cheryl 415 548-7723... 267-41-2394 O'Leary Michael 408 286-2428... 274-80-9391 Straight Dean 415 834-2919... 341-22-1782 Smith Meander 913 843-0462... 409-56-7008 Bennet Abraham 415 658-9932... 427-17-2319 Dull Ann 415 836-7128... 472-27-2349 Gringlesby Burt 707 938-6445... 486-29-1786 Locksley Charlene 415 585-4620... 527-72-3246 Greene Morningstar 615 297-2723... 648-92-1872 Blotchet-Halls Reginald 503 745-6402... 672-71-3249 Yokomoto Akiko 415 935-4228... 712-45-1867 del Castillo Innes 615 996-8275... 722-51-5454 DeFrance Michel 219 547-9982... 724-08-9931 Stringer Dirk 415 843-2991... 724-80-9391 MacFeather Stearns 415 354-7128... 756-30-7391 Karsen Livia 415 534-9219... 807-91-6654 Panteley Sylvia 301 946-8853... 846-92-7186 Hunter Sheryl 415 836-7128... 893-72-1158 McBadden Heather 707 448-4982... 899-46-2035 Ringer Anne 801 826-0752... 998-72-3567 Ringer Albert 801 826-0752... (23 row(s) affected)
  • 20. Select We can limit the columns returned by specifying them instead of using *. select au_lname, au_fname from authors au_lname au_fname ---------------------------------------- -------------------- White Johnson Green Marjorie Carson Cheryl O'Leary Michael Straight Dean Smith Meander Bennet Abraham Dull Ann Gringlesby Burt Locksley Charlene Greene Morningstar Blotchet-Halls Reginald Yokomoto Akiko del Castillo Innes DeFrance Michel Stringer Dirk MacFeather Stearns Karsen Livia Panteley Sylvia Hunter Sheryl McBadden Heather Ringer Anne Ringer Albert (23 row(s) affected)
  • 21. Select When you specify columns, you do not have to specify them in the order they appear in the table. You could have also executed the following: select au_fname, au_lname from authors au_fname au_lname -------------------- ---------------------------------------- Johnson White Marjorie Green Cheryl Carson Michael O'Leary Dean Straight Meander Smith Abraham Bennet Ann Dull Burt Gringlesby ... (23 row(s) affected)
  • 22. Concatenation We can also combine data together. This is called concatenation. We really want to display the first name and the last name separated by a space and then the rest of the data. The plus symbol (+) is the most widely used symbol for concatenation. (A double pipe || is sometimes used, but very rarely.) select au_fname+au_lname from authors JohnsonWhite MarjorieGreen CherylCarson MichaelO'Leary DeanStraight MeanderSmith AbrahamBennet AnnDull BurtGringlesby CharleneLocksley MorningstarGreene ReginaldBlotchet-Halls AkikoYokomoto Innesdel Castillo ... (23 row(s) affected)
  • 23. Concatenation cont. Concatenation is used for string (character) data. If a concatenation operator (+) is used on numeric data, the data is simply added together. select title_id,price,advance from titles title_id price advance -------- -------------------------- -------------------------- BU1032 19.99 5,000.00 BU1111 11.95 5,000.00 BU2075 2.99 10,125.00 BU7832 19.99 5,000.00 MC2222 19.99 0.00 ... (18 row(s) affected) select title_id,price+advance from titles title_id -------- -------------------------- BU1032 5,019.99 BU1111 5,011.95 BU2075 10,127.99 BU7832 5,019.99 MC2222 19.99 ... (18 row(s) affected)
  • 24. Select By combining the first select (selecting a constant) with the select on authors we did above, we can get some formatted output from the database. select au_fname+’ ‘ + au_lname, city + ’,’ + state + ’ ‘+zip from authors -------------------------------------- ----------------------------- Johnson White Menlo Park,CA 94025 Marjorie Green Oakland,CA 94618 Cheryl Carson Berkeley,CA 94705 Michael O'Leary San Jose,CA 95128 Dean Straight Oakland,CA 94609 Meander Smith Lawrence,KS 66044 Abraham Bennet Berkeley,CA 94705 Ann Dull Palo Alto,CA 94301 Burt Gringlesby Covelo,CA 95428 ... (23 row(s) affected) One of the many things you should take away from this class is the ability to put together a SQL statement like the one above. This simple principle saves DBAs hundreds of hours and year and makes their jobs much more simple.
  • 25. Aliases The title for our previous result set isn't too informative, and we really don't want to display our formula. We can rename or alias a column in two ways Use a space and then the alias Specify the keyword as and then the alias select au_fname+’ ‘ + au_lname, city + ’,’ + state + ’ ‘+zip Name_Address from authors select au_fname+’ ‘ + au_lname, city + ’,’ + state + ’ ‘+zip as Name_Address from authors
  • 26. Aliases We can apply aliases in two places within our SQL statements Select clause From clause By specifying an alias in the select clause we can rename the column headers for the output By specifying an alias in the from clause, we can save some typing and also perform some higher level queries which will require this. (This will be demonstrated in subsequent units.)
  • 27. Unit 1 Review A database is a collection of objects, the most prominent of which is a table. A table consists of rows/tuples/records and columns/attributes/fields. use <dbname> allows you to select a database SQL keywords are not case sensitive. Spacing and carriage returns are not needed. You can include a constant in your result set by hadding it just as you would a column An * allows you to select all columns in a table A + is used for concatenating two strings.
  • 28. Unit 1 Exercises Time allotted for exercises is 30 minutes