SlideShare une entreprise Scribd logo
1  sur  37
Télécharger pour lire hors ligne
T-SQL – 2nd session MedhatDawoud http://www.medhatdawoud.com MedhatDawoud@gmail.com @Med7atDawoud
Agenda Select the DBMS we will use Select Database to work on PUBS Database Databases and tables Connecting to your database Change databases Some rules for SQL Overview of PUBS database First Select Command
Agenda cont. Concatenate two columns Alias Where & compound criteria Comparison operators Range Wild cards Escape characters Pattern matching Negation
Database Management System There are many RDBMSs that is used such as: ,[object Object]
SQL server
MySQL
SQLite
…
etc.In this class we will use the SQL serveras our DBMS, Because it’s the most interactive one in out case.
SQL server 2005 or 2008 We need a SQLserver to interact with Database through queries as DBMS. Download from : http://www.microsoft.com/sqlserver/2005/en/us/express.aspx We need any version of SQL server(2005 or 2008).
Select Database to work on I selected the Northwindsimple database to make our queries on. You can download it from: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034
PUBS Database
Databases ,[object Object]
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.,[object Object]
Due to the differing terminologies, there are interchangeable sets of terms:Database	Mathematical	Data Processing 	Table		Relation	File 	Row		Tuple		Record 	Column	Attribute		Field ,[object Object],[object Object]
The same procedure gives the same result with the same database in any other DBMS.
Regardless of name, they all perform the same purpose.  This is to give you an interface for sending SQL statements and receiving results.,[object Object]
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 ,[object Object]
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.
The databases we have setup on our server are case insensitive.,[object Object]
select CustomerID…is the same as select  	CustomerID … ,[object Object],selectCustomerID…  This will give a syntax error, because SQL Server must be able to find your SQL keywords.
Rules cont. ,[object Object]
select CustomerID from Customers	is the same as select CustomerID 	from Customers ,[object Object]
The general format used by most people is to place the separate clausesof the statement on different lines,[object Object]
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.,[object Object]
Ok, Lets talk about commands (Queries)
Select ,[object Object]
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)
Select ,[object Object],select * ,[object Object],select * from authors ,[object Object]
We can limit the columns returned by specifying them instead of using *.select author_id , author_name from authors
Concatenation ,[object Object]

Contenu connexe

Tendances (20)

Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Database queries
Database queriesDatabase queries
Database queries
 
New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012 New T-SQL Features in SQL Server 2012
New T-SQL Features in SQL Server 2012
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Using T-SQL
Using T-SQL Using T-SQL
Using T-SQL
 
Sql a practical introduction
Sql   a practical introductionSql   a practical introduction
Sql a practical introduction
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
SQL Differences SQL Interview Questions
SQL Differences  SQL Interview QuestionsSQL Differences  SQL Interview Questions
SQL Differences SQL Interview Questions
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql basics
Sql basicsSql basics
Sql basics
 
SQL commands
SQL commandsSQL commands
SQL commands
 
12 SQL
12 SQL12 SQL
12 SQL
 
Introduction to mysql part 1
Introduction to mysql part 1Introduction to mysql part 1
Introduction to mysql part 1
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 

En vedette

Dating Game - T-SQL for High School Girls
Dating Game - T-SQL for High School GirlsDating Game - T-SQL for High School Girls
Dating Game - T-SQL for High School GirlsLynn Langit
 
MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008sqlserver content
 
70-461 Querying Microsoft SQL Server 2012
70-461 Querying Microsoft SQL Server 201270-461 Querying Microsoft SQL Server 2012
70-461 Querying Microsoft SQL Server 2012siphocha
 
10 Ways To Abuse T-SQL
10 Ways To Abuse T-SQL10 Ways To Abuse T-SQL
10 Ways To Abuse T-SQLTracy McKibben
 

En vedette (6)

Dating Game - T-SQL for High School Girls
Dating Game - T-SQL for High School GirlsDating Game - T-SQL for High School Girls
Dating Game - T-SQL for High School Girls
 
MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008
 
Mcts chapter 2
Mcts chapter 2Mcts chapter 2
Mcts chapter 2
 
70-461 Querying Microsoft SQL Server 2012
70-461 Querying Microsoft SQL Server 201270-461 Querying Microsoft SQL Server 2012
70-461 Querying Microsoft SQL Server 2012
 
Writing and optimizing T-SQL
Writing and optimizing T-SQLWriting and optimizing T-SQL
Writing and optimizing T-SQL
 
10 Ways To Abuse T-SQL
10 Ways To Abuse T-SQL10 Ways To Abuse T-SQL
10 Ways To Abuse T-SQL
 

Similaire à Intro to T-SQL – 2nd session

Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1iccma
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1Syed Asrarali
 
Ben Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxBen Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxStephenEfange3
 
Learning sql from w3schools
Learning sql from w3schoolsLearning sql from w3schools
Learning sql from w3schoolsfarhan516
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDSORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDSNewyorksys.com
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLMahir Haque
 
MS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A DatabaseMS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A Databasesqlserver content
 
MS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A DatabaseMS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A Databasesqlserver content
 
Intro to tsql unit 11
Intro to tsql   unit 11Intro to tsql   unit 11
Intro to tsql unit 11Syed Asrarali
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETEAbrar ali
 
Advanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptxAdvanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptxEllenGracePorras
 

Similaire à Intro to T-SQL – 2nd session (20)

Intro To TSQL - Unit 1
Intro To TSQL - Unit 1Intro To TSQL - Unit 1
Intro To TSQL - Unit 1
 
Intro to tsql unit 1
Intro to tsql   unit 1Intro to tsql   unit 1
Intro to tsql unit 1
 
Ben Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptxBen Finkel- Using the order by clause.pptx
Ben Finkel- Using the order by clause.pptx
 
Sql 2006
Sql 2006Sql 2006
Sql 2006
 
Learning sql from w3schools
Learning sql from w3schoolsLearning sql from w3schools
Learning sql from w3schools
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDSORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
ORACLE PL/SQL TUTORIALS - OVERVIEW - SQL COMMANDS
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
MYSQL.ppt
MYSQL.pptMYSQL.ppt
MYSQL.ppt
 
Mysql
MysqlMysql
Mysql
 
Retrieving Data From A Database
Retrieving Data From A DatabaseRetrieving Data From A Database
Retrieving Data From A Database
 
MS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A DatabaseMS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A Database
 
MS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A DatabaseMS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A Database
 
Intro to tsql unit 11
Intro to tsql   unit 11Intro to tsql   unit 11
Intro to tsql unit 11
 
Database COMPLETE
Database COMPLETEDatabase COMPLETE
Database COMPLETE
 
SAS Proc SQL
SAS Proc SQLSAS Proc SQL
SAS Proc SQL
 
75864 sql
75864 sql75864 sql
75864 sql
 
Advanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptxAdvanced Database Systems - Presentation 3.pptx
Advanced Database Systems - Presentation 3.pptx
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 

Plus de Medhat Dawoud

Plus de Medhat Dawoud (11)

Real time web
Real time webReal time web
Real time web
 
Using Git and BitBucket
Using Git and BitBucketUsing Git and BitBucket
Using Git and BitBucket
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linux
 
Select your career
Select your careerSelect your career
Select your career
 
Mesh cloud (road to mongoDB)
Mesh cloud (road to mongoDB)Mesh cloud (road to mongoDB)
Mesh cloud (road to mongoDB)
 
Before start
Before startBefore start
Before start
 
DevMix Startup
DevMix StartupDevMix Startup
DevMix Startup
 
Threading in C#
Threading in C#Threading in C#
Threading in C#
 
How to python
How to pythonHow to python
How to python
 
Program threats
Program threatsProgram threats
Program threats
 
Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
 

Dernier

4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
physiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxphysiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxAneriPatwari
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfChristalin Nelson
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Osopher
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...
Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...
Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...DrVipulVKapoor
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 

Dernier (20)

4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
physiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptxphysiotherapy in Acne condition.....pptx
physiotherapy in Acne condition.....pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
Healthy Minds, Flourishing Lives: A Philosophical Approach to Mental Health a...
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...
Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...
Geoffrey Chaucer Works II UGC NET JRF TGT PGT MA PHD Entrance Exam II History...
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 

Intro to T-SQL – 2nd session

  • 1. T-SQL – 2nd session MedhatDawoud http://www.medhatdawoud.com MedhatDawoud@gmail.com @Med7atDawoud
  • 2. Agenda Select the DBMS we will use Select Database to work on PUBS Database Databases and tables Connecting to your database Change databases Some rules for SQL Overview of PUBS database First Select Command
  • 3. Agenda cont. Concatenate two columns Alias Where & compound criteria Comparison operators Range Wild cards Escape characters Pattern matching Negation
  • 4.
  • 8.
  • 9. etc.In this class we will use the SQL serveras our DBMS, Because it’s the most interactive one in out case.
  • 10. SQL server 2005 or 2008 We need a SQLserver to interact with Database through queries as DBMS. Download from : http://www.microsoft.com/sqlserver/2005/en/us/express.aspx We need any version of SQL server(2005 or 2008).
  • 11. Select Database to work on I selected the Northwindsimple database to make our queries on. You can download it from: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034
  • 13.
  • 14. Databases come in all shapes and sizes. Some are large and some are small. But each database generally serves a particular purpose.
  • 15. Examples: Tracking employee payroll, sales data on a particular sales line, stock data for a particular industry
  • 16.
  • 17.
  • 18. The same procedure gives the same result with the same database in any other DBMS.
  • 19.
  • 20. 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.
  • 21.
  • 22. select is the same as SELECT is the same as SeLeCt, etc.
  • 23. However depending on the DBMS (Database Management System), the columns might be case sensitive.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 30.
  • 31. Ok, Lets talk about commands (Queries)
  • 32.
  • 33. 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.
  • 34. select 'Mary had a little lamb.'-------------- Mary had a little lamb. (1 row affected)
  • 35.
  • 36. We can limit the columns returned by specifying them instead of using *.select author_id , author_name from authors
  • 37.
  • 38. 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.
  • 39.
  • 40. We can rename or alias a column in two ways
  • 41. Use a space and then the alias
  • 42. Specify the keyword as and then the alias
  • 43. We can apply aliases in two places within our SQL statements
  • 46. Where So far we have returned the entire contents of a table. This is usually not very practical Suppose we wanted to see the authors that live in California. We could do a select * from authors and scroll through the result set looking for those where state = CA While feasible for a small table, this is not practical.
  • 47. Compound criteria This limited our result set to just those authors in CA But our list of authors could begin to get very large and we’re only looking for those authors with a last name of Green. We would do this with the following: select au_lname, au_fname, state from authors where state = 'CA' and lname = 'Green' Important Note : our DBMS might be case insensitive but the actual values of the data is case sensitive. So, Greennotequal to GREEN
  • 48. Compound criteria Now that we know how to get just those authors who live in CA, how do we get the authors that live in KS also? We accomplish this through the use of an OR instead of an AND select * from authors where state = 'CA' or state = 'KS'
  • 49. Compound criteria So, what is the difference between using an ANDand an OR? The AND is exclusive This means that the row must meet all of the conditions in order to be selected The OR is inclusive This means that for a row to be selected, it has to meet just one of the criteria
  • 50. Compound criteria You group by using parenthesis The proper SQL statement is as follows: select * from authors where (state = 'KS' and lname = 'Smith') or state = 'CA' Note that: this is differ from the group by clause that will be discussed later in this class
  • 51. Comparison Besides using an =, you can also use any of the other comparison operators: >, <, <=, >=,<>. If you want to get employees whose salary greater than 2000. It will be: select * from employees where salary > 2000
  • 52. Range Suppose we want to select all authors who live in CA, MI, KS, and UT.. What will you do ?? Instead of using multiple ORs, we can use an INoperator select au_lname, state from authors where state in ('CA','KS','MI','UT')
  • 53. Range We now want to select all books that have a price greater than or equal to $10, but also less than or equal to $20.. What will you do ? SQL has given us a betweenoperator select title_id, price from titles where price between 10 and 20
  • 54. Wild Cards SQL has two wildcard characters The percent (%) symbol designates any string of zero or more characters The underscore (_) designates a single character Suppose we wanted to select all authors whose first names start with M select au_fname, au_lname from authors where au_fname like 'M%'
  • 55. Escape Characters But what happens when we really want to find a % inside of the data To find this data we will employ an escape character select notes from titles where notes like '%@%%' escape '@' This tells the DBMS to treat the next character after the escape character (@) as a literal string
  • 56. Pattern Matching Suppose we wanted to retrieve all of the authors whose last names started with either an L, M, or S select au_lname, au_fname from authors where au_lname like '[LMS]%‘ We do not want to retrieve name names like McDay select au_lname, au_fname from authors where au_lname like '[A-Z][a-z][a-z][a-z]'
  • 57. Pattern Matching We now want to retrieve just those authors whose first name is four characters long select au_lname, au_fname from authors where au_fname like '____' (That's four underscore characters) --------------------------------------------------------------------- select * from authors where au_fname like '[^ ] [^ ] [^ ] [^ ]‘ The caret is a negation operator. The query above says to retrieve any first names that do not have a space as one of the four characters
  • 58. Negation We briefly touched on negation a few slides before. The negation operator is NOT or in patterns a caret (^) select au_fname, au_lname from authors where au_fname not like '[^ ] [^ ] [^ ] [^ ]' Select all authors who do not live in CA select * from authors where state <> 'CA'
  • 60. Ok Lets Query*  * Means to write queries in SQL server