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

Lecture 06 - MySQL-II

The document provides an overview of SQL Data Query Language (DQL) focusing on the SELECT statement and its various clauses such as WHERE, ORDER BY, and LIMIT. It includes exercises for practical application, covering functions like MIN(), MAX(), COUNT(), SUM(), and AVG(), as well as operators like IN and BETWEEN. Additionally, it explains the concept of subqueries and provides rules for their usage in SQL queries.

Uploaded by

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

Lecture 06 - MySQL-II

The document provides an overview of SQL Data Query Language (DQL) focusing on the SELECT statement and its various clauses such as WHERE, ORDER BY, and LIMIT. It includes exercises for practical application, covering functions like MIN(), MAX(), COUNT(), SUM(), and AVG(), as well as operators like IN and BETWEEN. Additionally, it explains the concept of subqueries and provides rules for their usage in SQL queries.

Uploaded by

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

DATABASE DESIGN

&
DEVELOPMENT
SQL PART - II

LAKSHIKA WANNINAYAKE

1
DQL - Data Query Language)
DQL statements are used for performing queries on the data within schema objects.
SELECT

2
SELECT Statement in SQL
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.

select specific fields available in the table select all the fields available in the table

3
Select with where clause
The WHERE clause is used to filter records.

It is used to extract only those records that fulfill a specified condition.

4
Operators in where clause

5
Exercise - 03
Assume you have the following records in the Students table:
id first_name last_name birth_year major
Computer
1 John Smith 2000
Science
2 Emily Johnson 2002 Biology
3 Michael Williams 2001 Psychology
4 Jessica Brown 2003 Engineering
5 Daniel Davis 2000 Mathematics

Retrieve the first name and last name of all students.


Get the full names (concatenation of first name and last name) of students whose birth year is after
2000.
Select the first name, last name, and major of students majoring in 'Biology' or 'Psychology'.

6
SQL ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.

7
Order by examples
SQL SELECT LIMIT Clause
The SELECT LIMIT clause is used to specify the number of records to return.
The SELECT LIMIT clause is useful on large tables with thousands of records. Returning a large
number of records can impact performance.
SQL SELECT DISTINCT Statement
The SELECT DISTINCT statement is used to return only distinct (different)
values which means to remove duplicate values.
SQL NULL Values
A field with a NULL value is a field with no value.

The IS NULL Operator

The IS NULL operator is used to test for empty values (NULL values).
SQL NULL Values contd.
The IS NOT NULL Operator
The IS NOT NULL operator is used to test for non-empty values (NOT NULL
values).
SQL MIN() and MAX() Functions
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
SQL COUNT() Function
The COUNT() function returns the number of rows that matches a specified
criterion.
SQL SUM() Function
Returns the total sum of a numeric column.
SQL AVG() Function
returns the average value of a numeric column.
Exercise
Write a SQL query to calculate the total sales amount for a table named sales. The table has two
columns: order_id and amount.
Calculate the sum of the amount to get the total sales.
Calculate the average of the total sales using SQL query.
SQL IN Operator
The IN operator allows you to specify multiple values in a WHERE clause.
SQL BETWEEN Operator
The BETWEEN operator selects values within a given range. The values can be numbers, text, or
dates.
The BETWEEN operator is inclusive: begin and end values are included.
MySQL Subquery
A subquery in MySQL is a query, which is nested into another SQL query and embedded with
SELECT, INSERT, UPDATE or DELETE statement along with the various operators.
A subquery is known as the inner query, and the query that contains subquery is known as the
outer query.
The following are the rules to use subqueries:

Subqueries should always be in parentheses.


If the main query does not have multiple columns for the subquery, then a subquery can have only
one column in the SELECT command.
We can use various comparison operators with the subquery, such as >, <, =, IN, ANY, SOME, and
ALL.
We cannot use the ORDER BY clause in a subquery, although it can be used inside the main query.

21
Exercise
Suppose you have an employees table with the following structure:
Employee Id is an auto increment unique key in the table

First name Last name department Hire date salary


John Doe Finance 2022-03-15 65,000
Emily Smith HR 2021-07-20 70,000
Michael Johnson Sales 2023-01-10 60,000
Sarah Brown Finance 2020-11-05 75,000
David Lee Marketing 2024-02-18 45,0000
1. Enter five sample data to the table.
2. Write a SQL query to retrieve all columns for all employees in the employees table.
3. Write a SQL query to retrieve the first_name, last_name, and salary columns for all employees.
4. Write a SQL query to retrieve all columns for employees in the 'Finance' department.
5. Write a SQL query to retrieve all columns for employees who were hired in 2021-12-31 or later.
6. Write a SQL query to retrieve all columns for employees with a salary greater than 66,000.
7. Write a SQL query to calculate the average salary of all employees.
8. Write a SQL query to retrieve all details of the employee with the highest salary.
9. Write a SQL query to count the number of employees in each department and display the
department name along with the count.

23
Thank you!

24

You might also like