Lecture 06 - MySQL-II
Lecture 06 - MySQL-II
&
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.
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
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 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:
21
Exercise
Suppose you have an employees table with the following structure:
Employee Id is an auto increment unique key in the table
23
Thank you!
24