Structured Query Language Basic Queries For Lab

simmis5 8 views 24 slides Oct 29, 2025
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

Aggregate functions are often used with the GROUP BY clause of the SELECT statement.
The GROUP BY clause splits the result-set into groups of values and the aggregate function can be used to return a single value for each group.


Slide Content

SQL LAB Ms Simmi S Assistant Professor Department of Computer Science UG Kristu Jayanti(Deemed To Be University Simmi S,Dept of CS

SQL Aggregate Functions An aggregate function is a function that performs a calculation on a set of values, and returns a single value. Aggregate functions are often used with the GROUP BY clause of the SELECT statement. The GROUP BY clause splits the result-set into groups of values and the aggregate function can be used to return a single value for each group. Simmi S,Dept of CS

SQL Aggregate Functions The most commonly used SQL aggregate functions are: MIN() - returns the smallest value within the selected column MAX() - returns the largest value within the selected column COUNT() - returns the number of rows in a set SUM() - returns the total sum of a numerical column AVG() - returns the average value of a numerical column Simmi S,Dept of CS

SQL Aggregate Functions MIN() - returns the smallest value within the selected column SELECT  MIN( column_name ) FROM   table_name WHERE   condition ; SELECT MIN (Marks) AS Lowest_Marks FROM Marks; Simmi S,Dept of CS

SQL Aggregate Functions MAX() - returns the largest value within the selected column SELECT  MAX( column_name ) FROM   table_name WHERE   condition ; SELECT MAX (Marks) AS Highest_Marks FROM Marks; Simmi S,Dept of CS

SQL Aggregate Functions COUNT() - returns the number of rows in a set SELECT   COUNT ( column_name ) FROM   table_name WHERE   condition ; SELECT COUNT(*) AS Total_Students FROM Student; Simmi S,Dept of CS

SQL Aggregate Functions SUM() - returns the total sum of a numerical column SELECT  SUM( column_name ) FROM   table_name WHERE   condition ; SELECT SUM (Marks) AS Total_Marks FROM Marks; Simmi S,Dept of CS

SQL Aggregate Functions AVG() - returns the average value of a numerical column SELECT  AVG( column_name ) FROM   table_name WHERE   condition ; SELECT AVG (Marks) AS Average_Marks FROM Marks; Simmi S,Dept of CS

The SQL LIKE Operator The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign % represents zero, one, or multiple characters The underscore sign _ represents one, single character Simmi S,Dept of CS

The SQL LIKE Operator SELECT   column1, column2, ... FROM   table_name WHERE   columnN   LIKE   pattern ; Select all customers that starts with the letter "a": SELECT  *  FROM  Customers WHERE   CustomerName   LIKE   'a%' ; Simmi S,Dept of CS

The SQL LIKE Operator Return all customers that ends with the pattern 'es': SELECT  *  FROM  Customers WHERE   CustomerName   LIKE   '%es’ ; Return all customers that  contains  the pattern ' mer ': SELECT  *  FROM  Customers WHERE   CustomerName   LIKE   '% mer %’ ; Return all customers with a  City  starting with any character, followed by " ondon ": SELECT  *  FROM  Customers WHERE  City  LIKE   '_ ondon ' ; Simmi S,Dept of CS

The SQL IN Operator The  IN  operator allows you to specify multiple values in a  WHERE  clause. The  IN  operator is a shorthand for multiple  OR  conditions. SELECT   column_name (s) FROM   table_name WHERE   column_name   IN  ( value1 ,  value2 , ...); Return all customers from 'Germany', 'France', or 'UK' SELECT  *  FROM  Customers WHERE  Country  IN  ( 'Germany' ,  'France' ,  'UK' ); Simmi S,Dept of CS

The 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.  SELECT   column_name (s) FROM   table_name WHERE   column_name   BETWEEN   value1   AND   value2; Selects all products with a price between 10 and 20: SELECT  *  FROM  Products WHERE  Price  BETWEEN   10   AND   20 ; Simmi S,Dept of CS

The SQL ORDER BY The ORDER BY keyword is used to sort the result-set in ascending or descending order. SELECT   column1 ,  column2, ... FROM   table_name ORDER   BY   column1, column2, ...  ASC | DESC ; SELECT  *  FROM  Products ORDER   BY  Price; Simmi S,Dept of CS

GROUP BY The  GROUP BY  statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The  GROUP BY  statement is often used with aggregate functions ( COUNT() ,  MAX() ,  MIN() ,  SUM() ,  AVG() ) to group the result-set by one or more columns. Simmi S,Dept of CS

GROUP BY The following SQL statement lists the number of customers in each country: SELECT   COUNT ( CustomerID ), Country FROM  Customers GROUP   BY  Country; SELECT   column_name (s) FROM   table_name WHERE   condition GROUP   BY   column_name (s) ORDER   BY   column_name (s); Simmi S,Dept of CS

The SQL HAVING Clause The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions. SELECT   column_name (s) FROM   table_name WHERE   condition GROUP   BY   column_name (s) HAVING   condition ORDER   BY   column_name (s); Simmi S,Dept of CS

The SQL HAVING Clause SELECT Stud_Dept , COUNT(*) AS Num_Students FROM student GROUP BY Stud_Dept HAVING COUNT(*) > 3; Simmi S,Dept of CS

SQL JOIN A  JOIN  clause is used to combine rows from two or more tables, based on a related column between them. (INNER) JOIN : Returns records that have matching values in both tables LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table FULL (OUTER) JOIN : Returns all records when there is a match in either left or right table Simmi S,Dept of CS

SQL JOIN Simmi S,Dept of CS

I NNER JOIN : Returns records that have matching values in both tables Simmi S,Dept of CS

LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table. Simmi S,Dept of CS

RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table. Simmi S,Dept of CS

FULL (OUTER) JOIN : Returns all records when there is a match in either left or right table Simmi S,Dept of CS
Tags