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

FoDB - Lab 3

Uploaded by

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

FoDB - Lab 3

Uploaded by

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

Lab 3

SQL Data Query Languages


Outline

• SELECT statement
• The FROM clause
• WHERE clause and SQL Operators
• GROUP BY clause
• HAVING clause
• ORDER BY clause
• Aggregate Functions

Fundamentals of Database System Lab 3;


2
By HG.
The SELECT statement

• SQL offers database users a powerful and flexible


data retrieval mechanism — the SELECT statement
• The General Form of the SELECT Statement:
• SELECT select_list
FROM source
WHERE condition(s)
GROUP BY expression
HAVING condition
ORDER BY expression

Fundamentals of Database System Lab 3;


3
By HG.
SQL Basics

• The SQL SELECT statement is used to fetch/retrieve


the data from a database table which returns this
data in the form of a result table. These result tables
are called result-sets.
• The basic syntax of the SELECT statement is :
– SELECT column1, column2, columnN FROM table_name;
• Example:
– SELECT EMPLOYEE_ID, LAST_NAME, SALARY FROM
EMPLOYEES;

Fundamentals of Database System Lab 3;


4
By HG.
SQL Basics …

• Example … :
– SELECT DEPARTMENT_NAME,LOCATION_ID FROM
DEPARTMENTS;
– SELECT DISTINCT job_id FROM employees;
• Retrieves unique job_id value
– SELECT * FROM EMPLOYEES;
• * is used to refer to all attributes of the table
– SELECT employee_id "Emp #", last_name
"Employee",job_id "Job", hire_date "Hire
Date‚ FROM employees;

Fundamentals of Database System Lab 3;


5
By HG.
SQL - WHERE Clause

• use the WHERE clause to filter the records and


fetching only the necessary records.
• The WHERE clause is not only used in the SELECT
statement, but it is also used in the UPDATE, DELETE
statement, etc.,
• Syntax:
– SELECT column1, column2, columnN
FROM table_name
WHERE [condition]

Fundamentals of Database System Lab 3;


6
By HG.
SQL - WHERE Clause …

• You can specify a condition using the comparison or


logical operators like >, <, =, LIKE, NOT, etc.
• Example:
– SELECT last_name, department_id
FROM employees
WHERE employee_id = 176;

– SELECT LAST_NAME, SALARY


FROM EMPLOYEES
WHERE SALARY > 2000;

Fundamentals of Database System Lab 3;


7
By HG.
SQL - WHERE Clause …

– SELECT last_name, salary


FROM employees
WHERE salary NOT BETWEEN 5000 AND 12000;

– SELECT last_name
FROM employees
WHERE last_name LIKE 'a%'
AND last_name LIKE '%e%';
-- retrieves those whose last name
begins with an a and contains ‘e’ in it.

Fundamentals of Database System Lab 3;


8
By HG.
SQL - WHERE Clause …

– SELECT last_name, job_id, salary


FROM employees
WHERE job_id IN ('SA_REP', 'ST_CLERK')
AND salary NOT IN (2500, 3500, 7000);

• Display the last name, job, and salary for all employees
whose job is either that of a sales representative or a
stock clerk, and whose salary is not equal to $2,500,
$3,500, or $7,000

Fundamentals of Database System Lab 3;


9
By HG.
SQL – ORDER BY Clause …
• The SQL ORDER BY clause is used to sort the data in
ascending or descending order, based on one or more
columns. Some databases sort the query results in an
ascending order by default.
• Example:
– SELECT last_name, job_id, salary
FROM employees
ORDER BY last_name;-- default in ascending

– SELECT last_name, job_id, salary


FROM employees
ORDER BY last_name DESC;

Fundamentals of Database System Lab 3;


10
By HG.
SQL - Aggregate Functions
• SQL has many built-in functions for performing processing on
string or numeric data. Following is the list of all useful SQL
built-in functions :
– The SQL COUNT aggregate function is used to count the number
of rows in a database table.
– The SQL MAX aggregate function allows us to select the highest
(maximum) value for a certain column.
– The SQL MIN aggregate function allows us to select the lowest
(minimum) value for a certain column.
– he SQL AVG aggregate function selects the average value for
certain table column.
– The SQL SUM aggregate function allows selecting the total for a
numeric column.

Fundamentals of Database System Lab 3;


11
By HG.
SQL - Aggregate Functions
• Examples:
– SELECT COUNT(DISTINCT manager_id) "Number of
Managers‚ FROM employees;

– SELECT MAX(salary) - MIN(salary) DIFFERENCE


FROM employees;

– SELECT job_id, ROUND(MAX(salary),0) "Maximum",


ROUND(MIN(salary),0) "Minimum",
ROUND(SUM(salary),0) "Sum",
ROUND(AVG(salary),0) "Average"
FROM employees
GROUP BY job_id;

Fundamentals of Database System Lab 3;


12
By HG.
SQL – GROUP BY Clause …

• The SQL GROUP BY clause is used in collaboration with the


SELECT statement to arrange identical data into groups. This
GROUP BY clause follows the WHERE clause in a SELECT
statement and precedes the ORDER BY clause.
• Example:
– SELECT job_id, COUNT(*)
FROM employees
GROUP BY job_id;
-- displays the number of people with the same job.

Fundamentals of Database System Lab 3;


13
By HG.
SQL – GROUP BY Clause …

• Example: ..
– SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id ;

– SELECT department_id, MAX(salary)


FROM employees
GROUP BY department_id;

Fundamentals of Database System Lab 3;


14
By HG.
SQL – GROUP BY Clause …

– SELECT manager_id, MIN(salary) SalLess6000

FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary) > 6000
ORDER BY MIN(salary) DESC;
-- displays the manager number and the salary of the lowest-paid
employee for that manager. Exclude any groups where the
minimum salary is $6,000 or less
-- The HAVING clause is used to specify conditions after grouping

Fundamentals of Database System Lab 3;


15
By HG.

You might also like