You are on page 1of 29

Queries

using Microsoft SQL


Server

PART 1

(cont.)
The SQL Computed Field (Column)

• A computed column does not exist


in database but can be computed
using the data in the existing
columns.
The SQL Computed Field (Column)

• Computations can involve any


Arithmetic Operator.

• Arithmetic Operators such as


Addition (+), Subtraction (-),
Multiplication (*) and Division (/)
The SQL Computed Field (Column)

• You can assign a name to the


computed field by using the word AS
keyword.

• Use a pair of square bracket [ ] for aliases


containing a space.
The SQL Computed Field (Column)

Example 15: Find the number,


name, and available credit
(CreditLimit - Balance) for each
customer.
SQL Code and Result

6
The SQL Computed Field (Column)

Example 16: Find the number,


name, and available credit for each
customer with at least $5,000 of
Available Credit.
SQL Code and Result

8
The SQL Computed Field (Column)

Example 17: Give the description and


OnHandValue (OnHand * Price) for
each part in item class HW.
SQL Code and Result

10
The SQL Functions

• SQL uses special functions, called


Aggregate Function to calculate sum,
average, count, maximum and
minimum values.

• These functions apply to group of


rows.
The SQL Aggregate Functions
FUNCTIONS DESCRIPTION
AVG Calculates the average value in a
column
COUNT Determines the number of rows in a
table
MAX Determine the maximum value in a
column
MIN Determine the minimum value in a
column
SUM Calculates a total of the values in a
column
Using the COUNT Function

Example 18: How many parts are


in item class HW?
SQL Code and Result

14
Using the COUNT Function

Example 19: How many


customer does sales rep 35
represent?
SQL Code and Result

16
Using the COUNT and SUM
Functions

Example 20: Count the number


of Premiere Products
customers and the total of their
balances.
SQL Code and Result

18
Using the AVG, Max and
Min Functions
Example 21: Find the total
balances of the Premiere
Products customer, as well as
the average balance, minimum
and maximum value of their
balances.
SQL Code and Result

20
Using the AVG, Max and
Min Functions
Example 22: What is the average
and total credit limit of all
customers of sales rep 65?
Also, get the largest and smallest
credit limit of the customers.
SQL Code and Result

22
The SQL GROUP BY

• Create groups of records that


share a common characteristic.

• GROUP BY command indicates


grouping in SQL

• SELECT – FROM – GROUP BY


Using Group By
Example 23: Group the record of
sales rep in the Premiere Products
database. What is the average
balance of each sales rep and
count the number of customers of
each representative. Sort by sales
rep number.
SQL Code and Result

25
Using Group By
Example 24: For each parts, list
the warehouse and the total units
on hand in each warehouse.
Group the record by warehouse
and order the record by
warehouse in descending order.
SQL Code and Result

27
To be continued next meeting . . .

SQL Joining Tables

28
Thank you for listening!

That in all things,


God may be glorified!

Prepared By: Engr. Lizel Rose Natividad

You might also like