Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Chapter Two SQL

The document discusses how to use string, mathematical and date functions in SQL queries to obtain required output. It provides examples of using functions like SUBSTR, CONCAT, LIKE, SUM, AVG, MAX, DATE_FORMAT, DATEDIFF and MONTH.

Uploaded by

Selam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Chapter Two SQL

The document discusses how to use string, mathematical and date functions in SQL queries to obtain required output. It provides examples of using functions like SUBSTR, CONCAT, LIKE, SUM, AVG, MAX, DATE_FORMAT, DATEDIFF and MONTH.

Uploaded by

Selam
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

2 )

O (
T W
E R
PT
H A
C

compiled by worku 15/5/23


t h e
i th
s w
t or
e r a e
o p en c
al
c rec e d
e ti
m
h rec t p
i t
r or
g a c
s in
U

compiled by worku 15/5/23


cont
In SQL Server, arithmetic operators also follow
the order of operations, or operator precedence, as
described earlier.
 The basic rules of operator precedence in SQL
Server are as follows:

1. Parentheses: expressions inside parentheses are


evaluated first.
compiled by worku 15/5/23
Cont…
. Exponentiation: exponentiation is evaluated next.

3. Multiplication and division: multiplication and


division are evaluated next, from left to right.

4. Addition and subtraction:


Addition and subtraction are evaluated last,
from left to right.

compiled by worku 15/5/23


Cont…
 For example, suppose you have the following expression:

 SELECT 2 + 3 * 4

 According to the order of operations, you should first multiply 3 by 4, and

then add the result to

2. Therefore, the result of the expression would be 14.

 If you want to change the order of operations, you can use parentheses to

group expressions together. For example, consider the following expression

 SELECT (2 + 3) * 4

compiled by worku 15/5/23


Cont…
 In this case, the addition operation inside the parentheses is

evaluated first, giving

5. Then, the multiplication is evaluated, giving a final result of

 It's important to be mindful of operator precedence when writing

SQL queries, as it can affect the results of your queries.

 If you're unsure about the order of operations, you can always

use parentheses to explicitly group expressions together and

control the order of evaluation.


compiled by worku 15/5/23
Cont…

It's important to be mindful of operator precedence


when writing SQL queries, as it can affect the results
of your queries.
 If you're unsure about the order of operations, you
can always use parentheses to explicitly group
expressions together and control the order of
evaluation.
compiled by worku 15/5/23
Using string functions and operators to
obtain required query output
 here are a few examples of how to use string
functions and operators in SQL queries to obtain the
required output:
 Using the SUBSTR function to extract a portion of a
string:
 Suppose you have a table employees with a
column full_name containing full names of employees,
and you want to extract the first name of each employee.
 You can use the SUBSTR function along with
the INSTR function to achieve this

compiled by worku 15/5/23


Cont….
 SELECT SUBSTR(full_name, 1, INSTR(full_name, ' ') - 1) AS

first_name FROM employees;

 n this example, the INSTR function returns the position of the

first space in the full_name column, and then


the SUBSTR function extracts the portion of the string before
the first space, which represents the first name.

 Using the CONCAT function to concatenate strings:

 Suppose you have a table customers with columns first_name

compiled by worku 15/5/23


Cont…
 Suppose you have a table customers with

columns first_name and last_name, and you want to create a new

column that contains the full name of each customer.

 You can use the CONCAT function to achieve this:

 SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM

customers;

 In this example, the CONCAT function concatenates

the first_name column, a space character, and the last_name column to

create a new column full_name.


compiled by worku 15/5/23
Cont…
 Using the LIKE operator to search for a specific
pattern in a string:
 Suppose you have a table products with a
column product_name, and you want to find all
products that contain the word "chair" in their name.
 You can use the LIKE operator along with
the % wildcard character to achieve this:
compiled by worku 15/5/23
Cont…
SELECT * FROM products WHERE
product_name LIKE '%chair%';
In this example, the LIKE operator with
the % wildcard character matches any string
that contains the word "chair" anywhere in
the product_name column.

compiled by worku 15/5/23


Mathematical Functions In SQL
Queries To Obtain The Required Output
 how to use mathematical functions in SQL queries to obtain
the required output:
 Using the SUM function to calculate the sum of values in a
column:
 Suppose you have a table sales with a column sales_amount,
and you want to calculate the total sales amount. You can use
the SUM function to achieve this:

compiled by worku 15/5/23


Cont…
 SELECT SUM(sales_amount) AS total_sales FROM sales;

 In this example, the SUM function adds up all the values in

the sales_amount column and returns the total as a single

value in a new column named total_sales.

 Using the AVG function to calculate the average of values

in a column:

compiled by worku 15/5/23


Cont…
Suppose you have a table employees with a
column salary, and you want to calculate the
average salary of all employees.
You can use the AVG function to achieve this:

SELECT AVG(salary) AS average_salary


FROM employees;

compiled by worku 15/5/23


Cont….
Using the MAX function to find the highest
value in a column:
Suppose you have a table products with a
column price, and you want to find the highest
price of all products.
 You can use the MAX function to achieve this:

compiled by worku 15/5/23


Cont…
 In this example, the MAX function finds the highest value in

the price column and returns the result as a single value in a


new column named highest_price.

 These are just a few examples of how to use mathematical

functions in SQL queries.

 There are many other mathematical functions available in

SQL that can be used to perform a wide range of


calculations and obtain the required output.
compiled by worku 15/5/23
use date functions in SQL queries to
obtain the required output:

Using the DATE function to format date values:

Suppose you have a table orders with a


column order_date containing date values in the
format yyyy-mm-dd, and you want to format the
date values to display only the year and month.
You can use the DATE function to achieve this:
compiled by worku 15/5/23
Cont…
 SELECT DATE_FORMAT(order_date, '%Y-%m') AS
year_month FROM orders;

 In this example, the DATE_FORMAT function formats

the order_date column to display only the year and month in


the format yyyy-mm, and returns the result as a new column
named year_month.

 Using the DATEDIFF function to calculate the difference

between two dates:


compiled by worku 15/5/23
Cont…
Suppose you have a table employees with
columns hire_date and termination_date, and you
want to calculate the number of days between the
two dates. You can use the DATEDIFF function to
achieve this:
SELECT DATEDIFF(termination_date, hire_date)
AS employment_duration FROM employees
compiled by worku 15/5/23
Cont…
In this example, the DATEDIFF function
calculates the number of days between
the termination_date and hire_date columns, and
returns the result as a new column
named employment_duration.
Using the MONTH function to extract the month
from a date value:
compiled by worku 15/5/23
Cont…
SELECT MONTH(order_date) AS order_month
FROM orders;
In this example, the MONTH function extracts
the month from each date value in
the order_date column, and returns the result as
a new column named order_month.

compiled by worku 15/5/23


Cont…

These are just a few examples of how to use


date functions in SQL queries.
 There are many other date functions available
in SQL that can be used to perform a wide
range of calculations and obtain the required
output.
compiled by worku 15/5/23

You might also like