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

Advanced Functions of SQL

The document discusses various SQL concepts including: 1) The GROUP BY statement which groups rows with the same values and is often used with aggregate functions like COUNT and SUM. 2) The ORDER BY statement which sorts data in ascending or descending order according to one or more columns. 3) Aggregate functions like COUNT, SUM, AVG, MIN, MAX which group values from multiple rows on certain criteria into a single value. 4) The HAVING clause which filters group results created by the GROUP BY clause.

Uploaded by

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

Advanced Functions of SQL

The document discusses various SQL concepts including: 1) The GROUP BY statement which groups rows with the same values and is often used with aggregate functions like COUNT and SUM. 2) The ORDER BY statement which sorts data in ascending or descending order according to one or more columns. 3) Aggregate functions like COUNT, SUM, AVG, MIN, MAX which group values from multiple rows on certain criteria into a single value. 4) The HAVING clause which filters group results created by the GROUP BY clause.

Uploaded by

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

Advanced functions of SQL

SQL GROUP BY Statement


• 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.
SQL | ORDER BY

• The ORDER BY statement in sql is used to sort the fetched data in


either ascending or descending according to one or more columns.
• By default ORDER BY sorts the data in ascending order.
• We can use the keyword DESC to sort the data in descending order and the
keyword ASC to sort in ascending order.
• Syntax:
• SELECT * FROM table_name ORDER BY column_name ASC|DESC
• SELECT * FROM table_name ORDER BY column1 ASC|DESC , column2
ASC|DESC
• SELECT * FROM Student ORDER BY ROLL_NO DESC;
• SELECT * FROM Student ORDER BY Age ASC , ROLL_NO DESC;
Aggregate functions in SQL

• In database management an aggregate function is a function where the


values of multiple rows are grouped together as input on certain criteria to
form a single value of more significant meaning.
• Various Aggregate Functions

• 1) Count()
• 2) Sum()
• 3) Avg()
• 4) Min()
• 5) Max()
SQL - Having Clause
• The HAVING Clause enables you to specify conditions that filter which group results
appear in the results.

• The WHERE clause places conditions on the selected columns, whereas the HAVING
clause places conditions on groups created by the GROUP BY clause.

• Syntax

• SELECT
• FROM
• WHERE
• GROUP BY
• HAVING
• ORDER BY
Having and Group By
• The HAVING clause must follow the GROUP BY clause in a query and must
also precede the ORDER BY clause if used. The following code block has the
syntax of the SELECT statement including the HAVING clause −

• SELECT column1, column2


• FROM table1, table2
• WHERE [ conditions ]
• GROUP BY column1, column2
• HAVING [ conditions ]
• ORDER BY column1, column2
SQL - Wildcard Operators
• We have already discussed about the SQL LIKE operator, which is used
to compare a value to similar values using the wildcard operators.
SQL - Alias Syntax

• You can rename a table or a column temporarily by giving another


name known as Alias.
SQL - Handling Duplicates
• There may be a situation when you have multiple duplicate records in
a table. While fetching such records, it makes more sense to fetch
only unique records instead of fetching duplicate records.

• The SQL DISTINCT keyword, which we have already discussed is used


in conjunction with the SELECT statement to eliminate all the
duplicate records and by fetching only the unique records.
SQL | SUB Queries
• In SQL a Subquery can be simply defined as a query within another
query.
• In other words we can say that a Subquery is a query that is embedded in
WHERE clause of another SQL query.
Important rules for Subqueries:
• You can place the Subquery in a number of SQL clauses: WHERE clause,
HAVING clause, FROM clause.
• Subqueries can be used with SELECT, UPDATE, INSERT, DELETE statements
along with expression operator. It could be equality operator or
comparison operator such as =, >, =, <= and Like operator.
• A subquery is a query within another query. The outer query is called as
main query and inner query is called as subquery.
• The subquery generally executes first, and its output is used to complete
the query condition for the main or outer query.
• Subquery must be enclosed in parentheses.
• Subqueries are on the right side of the comparison operator.
• ORDER BY command cannot be used in a Subquery
Copy the schema of table to another
• ///SELECT INTO can also be used to create a new, empty table using
the schema of another. Just add a WHERE clause that causes the
query to return no data:

• SELECT * INTO newtable


• FROM oldtable
• WHERE 1 = 0;

You might also like