Category Archives: MySql

MySQL Truncate

What if we only want to empty the data inside the table, and not to delete the table definition in this case we use TRUNCATE TABLE statement:

Syntax:

 

MySQL UNION Keyword

MySQL UNION Keyword allows you to combine two or more result sets from multiple tables or queries together.

  • The number of columns in each SELECT statement has to be the same .
  • The data type of the column in the column list of the SELECT statement must be the same or at least convertible.

MySQL UNION removes all duplicate rows from the result set

Syntax:

 

MySQL BETWEEN Clause

The BETWEEN operator is used in combination with WHERE clause match or select a range of data value between two values.

Syntax:

Consider “emp_info” table:

empID empName sale
1 Mark 1000
2 Max 1500
3 Mark 1800
5 John 1500

Output:

empID empName sale
1 Mark 1000
2 Max 1500
3 Mark 1800
4 Mark 3000
5 John 1500

MySQL IN Clause

The IN clause is used to match multiple values in a WHERE clause.

Syntax:

Consider “emp_info” table:

empID empName sale
1 Mark 1000
2 Max 1000
3 Mark 1000
4 Mark 1000
5 John 1000

Output:

empID empName sale
1 Mark 1000
2 Max 1000
3 Mark 1000
4 Mark 1000

MySQL Group By Clause

The GROUP BY statement is used to group the result set returned by SELECT statement by one or more columns.We can use GROUP BY to group values of a column we can perform any calculations on that column. COUNT, SUM, AVG etc. function can be applied on the grouped column.

Consider below “emp_info” table

empID empName sale
1 Mark 1000
2 Max 1000
3 Mark 1000
4 Mark 1000
5 John 1000

Output:

empName sale
Mark 3000
Max 1000
John 1000

MySQL NULL Values

We have seen SQL SELECT command along with WHERE clause to fetch data from MySQL table. But when we try to give a condition which compare field or column value to NULL it does not work properly.

To handle such situation MySQL provides three operators

  • IS NULL: operator returns true of column value is NULL.
  • IS NOT NULL: operator returns true of column value is not NULL.
  • <=> operator compare values, which (unlike the = operator) is true even for two NULL values

Conditions involving NULL are special. You cannot use = NULL or != NULL to look for NULL values in columns. Such comparisons always fail because it’s impossible to tell whether or not they are true. Even NULL = NULL fails.

To look for columns that are or are not NULL, use IS NULL or IS NOT NULL.

Using NULL values at Command Prompt:

Suppose a table tcount_tbl in TUTORIALS database and it contains two columns tutorial_author and tutorial_count, where a NULL tutorial_count indicates that the value is unknown:

Example:

Try out following examples:

You can see that = and != do not work with NULL values as follows:

To find records where the tutorial_count column is or is not NULL, the queries should be written like this:

Handling NULL Values in PHP Script:

You can use if…else condition to prepare a query based on NULL value.

Example:

Following example take tutorial_count from outside and then compare it with the value available in the table.

 

MySQL Sorting Results

The result set returned by the SELECT Query is free from any sort order unless you specify it in the SELECT statement.ORDER BY clause is used to sort the result set by a specified column or columns.The ORDER BY clause sorts the records in ascending order by default. To sort the result set in a descending order, use the DESC keyword.

Syntax:

  • We can sort returned result set on any column provided.
  • We can sort the result set on column or columns.
  • ASC or DESC keyword is used to get result in ascending or descending order accordingly. Default is ascending order.

 

MySQL Like Clause

The LIKE operator is used in a WHERE clause to search for a specified pattern of string in a column value, WHERE clause with equal sign (=) works fine where we want to do an exact match. Like if “emp_name = ‘deepmala'”. But there may be a requirement where we want to filter out all the results where emp_name should contain “mala”. This can be handled using SQL LIKE clause along with WHERE clause.

If LIKE clause is used along with % characters then sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.

Without a % character LIKE clause works similar to equal sign along with WHERE clause.

Syntax:

  • LIKE clause can be used with WHERE clause.
  • LIKE clause can be used in place of equal sign.
  • When LIKE is used alongwith % sign then sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.
  • We can have more than one conditions with AND or OR operators
  • LIKE clause can be used along in DELETE or UPDATE query too.

LIKE Operator Example

The “emp_info” table:

empID empName
1 smita
2 sneha
3 jyoti
4 pooja

Now we want to select the employees whose name starts with character “s” from the table above then the query would be like below.

OUTPUT:

empID empName
1 smita
2 sneha

MySQL Delete Query

If you want to delete a record from any MySQL table then you can use SQL command DELETE FROM.

MySql Delete Syntax:

WHERE clause can be used to delete specified rows, if WHERE clause is not specified then all rows get deleted.

 

MySQL Update Query

The MySQL UPDATE statement is used to update column values of existing rows in a table with new values.Specific columns can be modified using the SET clause by supplying new values for those column. MySQL UPDATE statement can be used with WHERE clause to specify the conditions for the rows to update. Without using WHERE clause, all rows are updated.

MySql UPDATE Syntax:

 

MySQL Where Clause

In the previous chapter SELECT statement is used to fetch data from MySQL table. Here we can add WHERE clause to filter out results. Using WHERE clause we can specify a selection condition to select only those records which fulfills the specified condition.

Syntax:

A WHERE clause can be used alongwith DELETE or UPDATE SQL command also.

MySQL Select Query

The SQL SELECT command is used to fetch data from MySQL database.

MySql SELECT Syntax:

  • You can use one or more tables separated by comma to include various condition using a WHERE clause. But WHERE clause is an optional part of SELECT command.
  • You can fetch one or more columns in a single SELECT query.
  • You can specify star (*) in place of fields. In this case SELECT will return all the fields
  • You can specify any condition using WHERE clause.
  • You can specify an offset using OFFSET from where SELECT will start returning records. By default offset is zero
  • You can limit the number of returned using LIMIT attribute.

Select all data

MySql select specific columns

MySql select specific rows

MySql select specific rows with AND operator

MySql select specific rows with OR operator

While fetching data you can write as complex SQL as you like. Procedure will remain same as mentioned above.

MySql select with LIMIT

The LIMIT clause can be used to limit the number of rows to be returned by the SELECT query. LIMIT takes one or two numeric arguments, which must both be non negative integer constants.With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):