0% found this document useful (0 votes)
26 views

SU2 Database File Handling2 Slides

The document discusses SQL select statements for retrieving data from database tables. It covers the basic SELECT statement syntax including specifying columns, tables, and the WHERE clause for filtering results. It also describes logical, string, and math operators that can be used in SELECT statements as well as sorting and wildcard operators. Sample queries are provided to demonstrate these SQL concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

SU2 Database File Handling2 Slides

The document discusses SQL select statements for retrieving data from database tables. It covers the basic SELECT statement syntax including specifying columns, tables, and the WHERE clause for filtering results. It also describes logical, string, and math operators that can be used in SELECT statements as well as sorting and wildcard operators. Sample queries are provided to demonstrate these SQL concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

SU 2

Database File
Handling
Topics

• 11.7 Selecting Data with the SQL Select Statement


• 11.8 Changing Data
11.7 Selecting Data with the SQL
Select Statement
• SQL, short for structured query language, is a standard language
for working with database management systems

• SQL statements consist of several keywords


– You use the keywords to construct statements known as queries
– Queries are sent to the DBMS as instructions to process data
– The SELECT and FROM statements, for example, are used for retrieving
the rows in a table. To retrieve the Description column for every row in
the Product table, use:
SELECT Description FROM Product

– SQL is not case-sensitive


• In this chapter, SQL statements are part of the C# applications you
will create
The Select Statement
• The SELECT statement allows you to select specific rows.
Its generic form is:
SELECT Columns FROM Table

• To retrieve the Description and Price columns for every


row in the Product table, use:
SELECT Description, Price FROM Product

• If you wish to retrieve every column in a table, use the *


character
SELECT * FROM Product
Specifying a Search Criteria with the
Where Clause
• When you need to narrow the list down to few selected
rows in the table, use the WHERE clause
– The general format is:
SELECT Columns FROM Table WHERE Criteria

• in which Criteria is a conditional expression


SELECT * FROM Product WHERE Price > 20.00
• SQL supports several relational operators for writing conditional
expressions Operator Meaning
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
Sample SQL Statements (Where
Clause)
• To retrieve the product numbers and prices of all the
items that are priced at $28.95:
SELECT Product_Number, Price FROM Product WHERE Price = 28.95

• To retrieve all the columns from only the rows where the
description is "Denim Jeans":
SELECT * FROM Product WHERE Description = 'Denim Jeans'

• If you need to include a single quote as part of a string,


simply write two single quotes. To search for Katy's Wool
Cap, use:
SELECT * FROM Product WHERE Description = 'Katy''s Wool Cap'
SQL String Functions
• SQL keywords and clauses are not case-sensitive. But, string
comparison are.
– 'Denim Jeans', 'denim jeans', and 'Denim jeans' are considered three
different string literals
– The following three SQL statements will generate three different results:
SELECT * FROM Product WHERE Description = 'Denim Jeans'
SELECT * FROM Product WHERE Description = 'denim jeans'
SELECT * FROM Product WHERE Description = 'Denim jeans'

• You can use the Lower() or Upper() string function before


performing the comparison

SELECT * FROM Product WHERE Lower(Description) = 'denim jeans'


SELECT * FROM Product WHERE Upper(Description) = 'DENIM JEANS'
Using the LIKE Operator
• The LIKE operator allows you to do a search based on a pattern
rather than specifying exactly what is desired
– "Oxford Cloth Shirt" and "Poplin Shirt" both contains the string "Shirt"
– Use the string "Shirt" as the pattern with the wildcard character %
– % represents any sequence of zero or more characters
SELECT * FROM Product WHERE Description LIKE '%Shirt%'
SELECT * FROM Product WHERE Description LIKE 'Chino%'
SELECT * FROM Product WHERE Description LIKE '%jeans'

– The underscore (_) wildcard character represents a single character. To


search for all rows in which Product_Number begins with "2", followed
by any one character, followed by "-0", followed by any one character,
use
SELECT * FROM Product WHERE Product_Number LIKE '2_-0_'
Using Logical Operators
• You can use the AND, OR, and NOT logical operators to
specify multiple search criteria in a WHERE clause
– The AND operator requires both search criteria be true for a row
to be qualified as a match
SELECT * FROM Product WHERE Price > 20.00 AND Price < 30.00

– The OR operator requires that either of the search criteria be true


for a row to be qualified as a match
SELECT * FROM Product WHERE Price > 20.00 OR Product_Number LIKE '10-%'

– The NOT operator disqualify a search criteria


SELECT * FROM Product WHERE Description NOT LIKE '%Shirt%'
Sorting the Results of a Select Query

• To sort the results of a SELECT query, use the ORDER BY


clause
SELECT * FROM Product ORDER BY Price
SELECT * FROM Product WHERE Price > 20.00 ORDER BY Price

– The results will be sorted in ascending order


– To sort in descending order, use the Desc
operator
SELECT * FROM Product ORDER BY Price Desc
SELECT * FROM Product WHERE Price > 20.00 ORDER BY Price Desc
SQL Math Functions
• SQL provides several functions for performing calculations
– Avg(Column): calculates the average value in a particular column
SELECT Avg(Price) FROM Product

– Sum(Column): calculates the sum of a column's values


SELECT Sum(Price) FROM Product

– Min(Column): finds the minimum value of a column.


SELECT Min(Price) FROM Product

– Max(Column): finds the maximum value of a column


SELECT Max(Price) FROM Product

– Count(Column): returns the number of values of the specified column


SELECT Count(Price) FROM Product

•To determine the number of rows in a table, use:


SELECT Count(*) FROM Product
Resources
Database Tutorials –
http://www.quackit.com/database/tutorial/
Accessing Data in Visual Studio –
http://msdn.Microsoft.com/en-
us/library/wzabh8c4.aspx
http://databases.about.com/od/tutorials/Tutorials
.htm
https://www.guru99.com/
Starting out with Visual C#, Tony Gaddis, 4th
edition, Chapter 11

You might also like