Más contenido relacionado
La actualidad más candente (20)
Sql xp 01
- 1. Querying Databases
Objectives
In this lesson, you will learn to:
Identify the features of client/server architecture
Identify the input requirements for a query
Plan and create a format for the query output
Identify and use sources of data for a query
Identify and use different types of queries
Use the SELECT statement with logical operators
©NIIT SQL/lesson 1/Slide 1 of 50
- 2. Querying Databases
Getting Started
Client/Server Architecture
In this architecture, the functionality of the application is
split between two processes: the client process and the
server process
The client process handles the data input and the user
interface issues
The server process performs all validations on the data
©NIIT SQL/lesson 1/Slide 2 of 50
- 3. Querying Databases
Getting Started (Contd.)
Benefits of the two-tier model (Client/Server) model are:
Data Sharing: Data is separated from the client side and
is stored at a central location where all users have
access to the data.
Reduces duplication and maintenance: Since data is
stored centrally, data maintenance is easy. There is no
duplication of data and therefore no inconsistency in the
data stored.
©NIIT SQL/lesson 1/Slide 3 of 50
- 4. Querying Databases
Getting Started (Contd.)
Features of MS SQL Server
Microsoft SQL Server provides various features to manage
data. Some of the features offered by Microsoft SQL Server
are:
® Web-Enabled
® Scalable and Reliable
® Fastest Time-to-Market
©NIIT SQL/lesson 1/Slide 4 of 50
- 5. Querying Databases
Getting Started (Contd.)
Structured Query Language
SQL Server provides the Structured Query Language
(SQL) to access data objects from the server
MS-SQL Server has added more features to the base-level
syntax of SQL and has its own SQL called Transact-SQL
(T-SQL)
Datatypes in SQL Server
SQL Server can store various types of data like char,
money, int, or datetime
©NIIT SQL/lesson 1/Slide 5 of 50
- 6. Querying Databases
1.D.1 Displaying Specific Attributes From a Table
The details regarding recruitment are stored in the
RECRUITMENT database on the local SQL server. These
tables contain data on candidates, their qualifications and
contact details, the vacancies available, and other information
required by the recruitment agency. A report containing the
name, city, and telephone number of all the external
candidates is required.
©NIIT SQL/lesson 1/Slide 6 of 50
- 7. Querying Databases
Task List
Create a format for the query output
Draft the query
Execute the query
Verify that the query output is as per the required results
©NIIT SQL/lesson 1/Slide 7 of 50
- 8. Querying Databases
Create a format for the query output
Result:
The output required from the query is the specific details
of all the external candidates
The column headings required in the report are the name,
city, and telephone number of the external candidates
The format of the report is as follows:
vFirstName vLastName cCity cPhone
©NIIT SQL/lesson 1/Slide 8 of 50
- 9. Querying Databases
Draft the query
The SELECT Statement
Enables you to access and retrieve data from a database
Syntax:
SELECT [ALL | DISTINCT] select_column_list
[INTO [new_table_name]]
[FROM {table_name | view_name}
[(optimizer_hints)][[, {table_name2 |
view_name2}[(optimizer_hints)]
[WHERE clause]
[GROUP BY clause][HAVING clause]
[ORDER BY clause][COMPUTE clause]
[FOR BROWSE]
©NIIT SQL/lesson 1/Slide 9 of 50
- 10. Querying Databases
Draft the query (Contd.)
Using Literals
The result set of a data query statement can be made
more readable by including a string called a literal in the
SELECT list
Literals are enclosed in single quotes and get printed
exactly as they are written in the SELECT list
©NIIT SQL/lesson 1/Slide 10 of 50
- 11. Querying Databases
Draft the query (Contd.)
Result:
The information is available in the Recruitment database
The information available in the table ExternalCandidate is
required
The specific attributes required from the ExternalCandidate
table are vFirstName, vLastName, cCity, and cPhone
©NIIT SQL/lesson 1/Slide 11 of 50
- 12. Querying Databases
Execute the query
Action:
Connect to the server
Connect to the database
® Method 1: Choose the database (RECRUITMENT)
from the SQL Query Analyzer database drop-down list
® Method 2: In the Query Analyzer window, type:
USE RECRUITMENT
©NIIT SQL/lesson 1/Slide 12 of 50
- 13. Querying Databases
Execute the query (Contd.)
In the Query Analyzer window, type:
SELECT vFirstName, vLastName, cCity,
cPhone
FROM ExternalCandidate
Execute the query
® Click
the Execute option from the Query menu on the
menu bar
or
® Click the Execute button on the toolbar
©NIIT SQL/lesson 1/Slide 13 of 50
- 14. Querying Databases
Verify that the query output is as per the required
results
Check whether:
The required columns are displayed
©NIIT SQL/lesson 1/Slide 14 of 50
- 15. Querying Databases
Just a Minute…
In Tebisco, there is a review by senior management on
manpower. You need to write a query to display a list of the
budgeted and the actual people available for various roles
from the Recruitment database.
©NIIT SQL/lesson 1/Slide 15 of 50
- 16. Querying Databases
1.D.2 Displaying Specific Columns With User-Friendly
Column Headings
The percentage charge of contract recruiters for hiring
candidates is under review. The names of the contract
recruiters and their hire charges are required. The report
should contain user-friendly column headings as specified
in the following format:
Recruiter Name Hire Charge
©NIIT SQL/lesson 1/Slide 16 of 50
- 17. Querying Databases
Task List
Draft the query
Execute the query
Verify that the query output is as per the required results
©NIIT SQL/lesson 1/Slide 17 of 50
- 18. Querying Databases
Draft the query
Displaying Columns With User-Defined Headings
Syntax
® Method 1:
SELECT column_heading=column_name[,column
name…] FROM table_name
® Method 2:
SELECT column_name column_heading
[,column_name…] FROM table_name
©NIIT SQL/lesson 1/Slide 18 of 50
- 19. Querying Databases
Draft the query (Contd.)
Result:
The information is available in the table ContractRecruiter
The columns required are cName, siPercentageCharge with the
column headings 'Recruiter Name' and 'Hire Charge' from the
table
©NIIT SQL/lesson 1/Slide 19 of 50
- 20. Querying Databases
Execute the query
Action:
In the Query Analyzer window, type:
SELECT 'Recruiter Name' = cName, 'Hire
Charge' = siPercentageCharge
FROM ContractRecruiter
Execute the query
©NIIT SQL/lesson 1/Slide 20 of 50
- 21. Querying Databases
Verify that the query output is as per the required
results
Check whether:
The required columns are displayed
The required column headings are displayed
©NIIT SQL/lesson 1/Slide 21 of 50
- 22. Querying Databases
1.D.3 Displaying Selected Rows From a Table
Campus recruitment by Tebisco needs to be conducted in
the state of California. Therefore, details of all colleges in
the state of California are required.
©NIIT SQL/lesson 1/Slide 22 of 50
- 23. Querying Databases
Task List
Create a format for the query output
Draft the query
Execute the query
Verify that the query output is as per the required results
©NIIT SQL/lesson 1/Slide 23 of 50
- 24. Querying Databases
Create a format for the query output
Result:
The output required is the details of all colleges in the
state of California
The format of the report is shown below:
cCollegeCode cCollegeName vCollegeAddress cCity cState cZip cPhone
©NIIT SQL/lesson 1/Slide 24 of 50
- 25. Querying Databases
Draft the query
The SELECT…WHERE Statement
Retrieves and displays data with a specific condition
Syntax
SELECT column_list FROM table_name
WHERE search_condition
©NIIT SQL/lesson 1/Slide 25 of 50
- 26. Querying Databases
Draft the query (Contd.)
Result:
® The information is available in the College table
® The column headings are the attribute names of the
College table
® Thecondition is that the state is ‘California’, that is,
cState=‘California‘
® Therefore, the query using the SELECT statement
should be:
SELECT * FROM College
WHERE cState = 'California'
©NIIT SQL/lesson 1/Slide 26 of 50
- 28. Querying Databases
Verify that the query output is as per the required
results
Check whether:
All columns are displayed
Only rows in which the value of the cState attribute is
California are displayed
©NIIT SQL/lesson 1/Slide 28 of 50
- 29. Querying Databases
Just a Minute…
The hiring charges of contract recruiters (charging more than
8 percent of the annual salary as their percentage) are
under review. The names, addresses, and the hiring
charges of these contract recruiters are required. Write a
query to display the required information.
©NIIT SQL/lesson 1/Slide 29 of 50
- 30. Querying Databases
Arithmetic Operators
SQL Server supports operators that perform arithmetic
operations such as addition, subtraction, division, and
multiplication on numeric columns
The arithmetic operators supported by SQL Server are:
+ (for Addition)
- (for Subtraction)
/ (for Division)
* (for Multiplication)
% (for Modulo)
©NIIT SQL/lesson 1/Slide 30 of 50
- 31. Querying Databases
Arithmetic Operators (Contd.)
Some rules regarding the use of arithmetic operators are:
Arithmetic operations can be performed on numeric
columns or numeric constants
The Modulo (%) operator cannot be used with columns of
money, smallmoney, float, or real datatypes
©NIIT SQL/lesson 1/Slide 31 of 50
- 32. Querying Databases
Arithmetic Operators (Contd.)
Operator Precedence
When multiple arithmetic operators are used in a single
query, the processing of the operation takes place
according to the precedence of arithmetic operators
The precedence level of arithmetic operators in an
expression is multiplication (*), division (/), modulo (%)
followed by subtraction (-) and addition (+)
The precedence of the operators can be changed by using
the primary grouping object called parentheses (())
©NIIT SQL/lesson 1/Slide 32 of 50
- 33. Querying Databases
Search Based on Conditions
SQL Server provides a few methods for searching rows in a
table. These methods can be broadly categorized as follows:
Logical operators
Comparison operators
Range operators
List operators
©NIIT SQL/lesson 1/Slide 33 of 50
- 34. Querying Databases
1.D.4. Displaying Rows That Satisfy Multiple Conditions
Candidates who have scored marks between 80 and 100 in a
test need to be called for an interview. Therefore, a report
displaying the names of candidates and their scores is
required
©NIIT SQL/lesson 1/Slide 34 of 50
- 35. Querying Databases
Task List
Create a format for the query output
Draft the query
Execute the query
Verify that the query output is as per the required results
©NIIT SQL/lesson 1/Slide 35 of 50
- 36. Querying Databases
Create a format for the query output
Result:
The output requirements for the report are the names and
the test scores of only those candidates who have scored
marks between 80 and 100 in a test
The format of the report is shown below:
vFirstName vLastName siTestScore
©NIIT SQL/lesson 1/Slide 36 of 50
- 37. Querying Databases
Draft the query
Logical Operator - Multiple search conditions can be combined
by using the following logical operators:
ORreturns the result when any of the specified search
conditions is true
ANDreturns the result when all the specified search
conditions are true
NOT neutralizes the expression that follows it
When more than one logical operator is combined in the
WHERE clause, the order of precedence is NOT, AND, and
OR
Parentheses can be used to change the logical order of
precedence
©NIIT SQL/lesson 1/Slide 37 of 50
- 38. Querying Databases
Draft the query (Contd.)
Syntax
SELECT column_list FROM table_name WHERE
conditional_expression{AND/OR}[NOT]
conditional_expression
©NIIT SQL/lesson 1/Slide 38 of 50
- 39. Querying Databases
Draft the query (Contd.)
Result:
The required information is available in the
ExternalCandidate table
The column headings are the attribute names of the
relevant columns in the ExternalCandidate table
The condition to be satisfied is that the test score should be
greater than or equal to 80 (siTestScore = 80) and less
than or equal to 100 (siTestScore = 100)
Therefore, the query using the SELECT statement should
be:
SELECT vFirstName, vLastName, siTestScore
FROM ExternalCandidate
WHERE siTestScore = 80 AND siTestScore =
©NIIT 100 SQL/lesson 1/Slide 39 of 50
- 41. Querying Databases
Verify that the query output is as per the required
results
Check whether:
The required columns are displayed
All the rows displayed have values of siTestScore
between 80 and 100
©NIIT SQL/lesson 1/Slide 41 of 50
- 42. Querying Databases
More on Operators
Apart from logical operators, SQL Server also supports the
following operators:
Comparison Operators
Range Operators
List Operators
©NIIT SQL/lesson 1/Slide 42 of 50
- 43. Querying Databases
More on Operators (Contd.)
Comparison Operators
Comparison operators allow row retrieval from a table
based on the condition specified in the WHERE clause
Syntax
SELECT column_list FROM table_name
WHERE expression1 comparison_operator
expression2
©NIIT SQL/lesson 1/Slide 43 of 50
- 44. Querying Databases
More on Operators (Contd.)
Range Operators
The range operator is used to retrieve data that can be
extracted in ranges. The range operators are:
® BETWEEN
® NOT BETWEEN
Syntax
SELECT column_list FROM table_name
WHERE expression1 range_operator
expression2 AND expression3
©NIIT SQL/lesson 1/Slide 44 of 50
- 45. Querying Databases
More on Operators (Contd.)
List Operators
The IN operator allows the selection of values that match
any one of the values in a list
The NOT IN operator restricts the selection of values that
match any one of the values in a list
Syntax
SELECT column_list FROM table_name
WHERE expression list_operator (‘value_list‘)
©NIIT SQL/lesson 1/Slide 45 of 50
- 46. Querying Databases
Just a Minute…
In order to identify the successful candidates, who took the
test between March 5, 2001 and March 12, 2001, a report
displaying the names of candidates and their scores needs
to be created. The format of the report is as follows:
vFirstName vLastName siTestScore dTestDate
©NIIT SQL/lesson 1/Slide 46 of 50
- 47. Querying Databases
Summary
In this lesson, you learned that:
A database management system consists of a Server, a
database (or multiple databases) with tables containing data,
and a client (front-end) that helps a user interact with the
server to retrieve data.
The language provided by SQL Server to access data from a
database is known as Structured Query Language (SQL).
Microsoft SQL Server provides a customized implementation
of SQL called T-SQL.
©NIIT SQL/lesson 1/Slide 47 of 50
- 48. Querying Databases
Summary (Contd.)
SQL Server provides a SELECT statement to access and
retrieve data from a database. The SELECT statement
queries the server to prepare a result and return it to the
client application.
The SELECT statement can be used to retrieve specific
column(s) from the table by specifying the column names
from the table.
The SELECT statement along with the asterisk (*) symbol
produces the result that contains the details of all the
columns in the table.
©NIIT SQL/lesson 1/Slide 48 of 50
- 49. Querying Databases
Summary (Contd.)
The order of the columns can be changed in the result set of
the SELECT statement by specifying the individual column
names separated by a comma.
SQL Server provides two methods for specifying the column
heading. In the first method, the column heading is specified
before the column name whereas in the second method, the
column name is specified before the column heading.
The WHERE clause is provided by SQL Server to specify
the condition to retrieve specific data.
The result set of the data query statement can be made
more readable by including a string called literals in the
SELECT list.
©NIIT SQL/lesson 1/Slide 49 of 50
- 50. Querying Databases
Summary (Contd.)
SQL Server supports operators that perform arithmetic
operations like addition, subtraction, division, and
multiplication on numeric columns.
In a mixed mode arithmetic operation, the lower datatype
value gets converted into a higher datatype value according
to datatype precedence.
SQL Server provides the following set of operators:
Logical operators like AND, OR and NOT
Comparison operators like =, , , =, =, ! =, ! and !
Range operators like BETWEEN and NOT BETWEEN
List operators like IN and NOT IN
©NIIT SQL/lesson 1/Slide 50 of 50