PHP Tutorials
- What is MySqli
- mysql query
- mysql query example
- InnoDB
- mysql column Type
- CRUD Example
- Connection Using Function
- mysql keys
- SELECT
- WHERE
- UPDATE
- Count no of Rows
- ALIAS
- AND, AND & OR
- BETWEEN
- COMPARISON OPERATOR
- DELETE
- DELETE LIMIT
- DISTINCT
- EXISTS
- FROM
- GROUP BY
- HAVING
- IN
- INTERSECT
- IS NULL & IS NOT NULL
- LIKE
- NOT
- ORDER BY
- SELECT LIMIT
- SUBQUERY
- TRUNCATE
- UNION && UNION ALL
- Concat & Group_Concat
- mysql Function
- Mysql Insert Id
- MySql Aggregate Function
- Mysql Join
- JOIN in MySql
- Trigger
- Procedure
- Transaction
- views
- Index
- SQL Injection
- Normalization
- Query Bind
- Interview Questions
Important Link
Insert Query :-
Useful aggregate functions:
SQL Aggregate Functions.
SQL aggregate functions return a single value, calculated from values in a column.
SQL Scalar functions
SQL scalar functions return a single value, based on the input value.
AVG() Function :-
AVG() function returns the average value of a numeric column.
Example :-
SELECT AVG(column_name) FROM table_name;
SELECT AVG(cost) AS Costaverage FROM Products;
SELECT itemname, itemprice FROM Products WHERE cost>(SELECT AVG(cost) FROM Products);
COUNT() Function :-
COUNT() function returns the number of rows that matches a specified criteria.
COUNT(*) Function :-
COUNT(*) function returns the number of records in a table:
COUNT(DISTINCT column_name) Function :-
COUNT(DISTINCT column_name) function returns the number of distinct values of the specified column.
limit keyword:-
limit keyword gives no of row from table which you specify.
MAX() Function:-
The MAX() function returns the highest value of the selected column.
Syntax :Example :-
select MAX(Salary) from Employee WHERE Salary <> (select MAX(Salary) from Employee );
MIN() Function:-
The MIN() function returns the LOWEST value of the selected column.
Syntax :HAVING Clause:-
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
The MySQL HAVING clause is used in the SELECT statement to specify filter conditions for a group of rows or aggregates.
The MySQL HAVING clause is often used with the GROUP BY clause. When using with the GROUP BY clause, we can apply a filter condition to the columns that appear in the GROUP BY clause. If the GROUP BY clause is omitted, the HAVING clause behaves like the WHERE clause.
Notice that the HAVING clause applies the filter condition to each group of rows, while the WHERE clause applies the filter condition to each individual row.
GROUP BY Statement:-
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
Syntax :Round Function.
The ROUND() function is used to round a numeric field to the number of decimals specified.
Syntax :-Between keyword.
he BETWEEN operator selects values within a range. The values can be numbers, text, or dates.
Syntax :-SELECT * FROM Products WHERE Price BETWEEN 10 AND 20;
SELECT * FROM Products WHERE Price NOT BETWEEN 10 AND 20;
SQL Aliases :-
SQL aliases are used to temporarily rename a table or a column heading.
Basically aliases are created to make column names more readable
Syntax :-Count duplicate data in table.
FROM duplicate
GROUP BY salary
HAVING COUNT(*) > 1";
CAST()
The CAST() function is used to convert the type of an expression to a specified type. The function is similar to CONVERT() function.
CAST(expr AS type))
SELECT first_name, CAST(hire_date AS DATETIME) 'Joining Date' from employees where hire_date>('1987-09-15') ;
SELECT enum_col FROM table_name ORDER BY CAST(enum_col AS CHAR);
COALESCE Function
COALESCE function returns the first non-NULL value from the given list. If there are no non-null values The COALESCE function returns NULL.
we use COALESCE function with the City column. Instead of showing NULL, we are showing NA where the city column value is null.
SELECT Id,
COALESCE(FirstName, MiddleName, LastName) AS Name,
Gender, AGE, COALESCE(City, 'NA') AS City
FROM Employee;
we use COALESCE function with First, Middle, and Last Name column values.
SELECT Id, COALESCE(FirstName, MiddleName, LastName) AS Name, Gender, AGE, City FROM Employee;
CONCAT function
The CONCAT function can take n number of strings as arguments and return a single string as a result by adding all the input strings.
CONCAT (string1, string2, string3, …)
The CONCAT function take one or more arguments.
It concatnate all arguments string and returns a single string as a result.
The CONCAT function returns a binary string if the arguments include any binary strings.
If the argument is numeric, it is converted to its equivalent nonbinary string form.
Returns NULL if any argument is NULL.
SELECT CONCAT(‘MySQL’, ‘ITTutorials’);
// MySQLITtutorials
SELECT CONCAT(‘MySQL’, ‘ ‘, ‘Tutorials’);