MySQL Programming

  Home  Databases Programming  MySQL Programming


“Learn MySQL Programming with interview questions and answers”



110 MySQL Programming Questions And Answers

61⟩ How To Enter Numeric Values as HEX Numbers?

If you want to enter numeric values as HEX numbers, you can quote HEX numbers with single quotes and a prefix of (X), or just prefix HEX numbers with (0x). A HEX number string will be automatically converted into a numeric value, if the expression context is a numeric value. Here are some good examples:

SELECT X'10' + 16 FROM DUAL;

32

SELECT 0x1000 + 0 FROM DUAL;

4096

 209 views

62⟩ How To Calculate Expressions with SQL Statements?

There is no special SQL statements to calculate expressions. But you can use the "SELECT expression FROM DUAL" statement return the calculated value of an expression. "DUAL" is a dummy table in the server. The tutorial exercise below shows you some good examples:

SELECT 'Hello world!' FROM DUAL;

Hello world!

SELECT (1+2)*3/4 FROM DUAL;

2.2500

SELECT TRUE FROM DUAL;

1

SELECT TRUE AND FALSE FROM DUAL;

0

SELECT TIME(SYSDATE()) FROM DUAL;

21:30:26

 209 views

63⟩ How To Include Character Strings in SQL statements?

If you want to include character strings in your SQL statements, you need to quote them in one of the following formats:

► Using single quotes. For example 'GlobalGuideLine.com'.

► Using double quotes. For example "ggl Center".

► Using single quotes prefixed with N for NATIONAL characters (same as UTF8 characters). For example N'Allo, Francois.'.

► Using single quotes prefixed with _utf8 for UTF8 characters. For example _utf8'Allo, Francois.'.

 209 views

64⟩ How To Enter Characters as HEX Numbers?

If you want to enter characters as HEX numbers, you can quote HEX numbers with single quotes and a prefix of (X), or just prefix HEX numbers with (0x). A HEX number string will be automatically converted into a character string, if the expression context is a string. Here are some good examples:

SELECT X'313233' FROM DUAL;

123

SELECT 0x414243 FROM DUAL;

ABC

SELECT 0x46594963656E7465722E636F6D FROM DUAL;

GlobalGuideLine.com

 211 views

65⟩ What Are the Differences between CHAR and VARCHAR?

CHAR and VARCHAR are both ASCII character data types by default. But they have the following major differences:

► CHAR stores values in fixed lengths. Values are padded with space characters to match the specified length.

► VARCHAR stores values in variable lengths. Values are not padded with any characters. But 1 or 2 extra bytes are added to store the length of the data.

The table below shows you a good comparison of CHAR and VARCHAR data types:

Value CHAR(4)  Length 

'' ' ' 4 bytes

'ab' 'ab ' 4 bytes

'abcd' 'abcd' 4 bytes

Value VARCHAR(4) Length

'' '' 1 byte

'ab' 'ab' 3 bytes

'abcd' 'abcd' 5 bytes

 193 views

66⟩ How To Concatenate Two Character Strings?

If you want concatenate multiple character strings into one, you need to use the CONCAT() function. Here are some good examples:

SELECT CONCAT('Welcome',' to') FROM DUAL;

Welcome to

SELECT CONCAT('ggl','center','.com') FROM DUAL;

GlobalGuideLine.com

 199 views

67⟩ How To Enter Binary Numbers in SQL Statements?

If you want to enter character strings or numeric values as binary numbers, you can quote binary numbers with single quotes and a prefix of (B), or just prefix binary numbers with (0b). Binary numbers will be automatically converted into character strings or numeric values based on the expression contexts. Here are some good examples:

SELECT B'010000010100001001000011' FROM DUAL;

ABC

SELECT 0b1000 + 0 FROM DUAL;

8

 193 views

68⟩ What Are NULL Values?

NULL is a special value that represents no value. Here are basic rules about NULL values:

► NULL presents no value.

► NULL is not the same as an empty string ''.

► NULL is not the same as a zero value 0.

► NULL can be used as any data type.

► NULL should not be used in any comparison options.

► NULL has its own equality operator "IS".

► NULL has its own not-equality operator "IS NOT".

The tutorial exercise shows you some interesting examples:

SELECT 0 IS NULL FROM DUAL;

0

SELECT 0 IS NOT NULL FROM DUAL;

1

SELECT '' IS NULL FROM DUAL;

0

SELECT '' IS NOT NULL FROM DUAL;

1

SELECT NULL IS NULL FROM DUAL;

1

SELECT NULL IS NOT NULL FROM DUAL;

0

 203 views

69⟩ What Are String Data Types?

MySQL supports the following string data types:

► CHAR(n) same as CHARACTER(n) - Fixed width and " " padded characters strings. Default character set is ASCII.

► NCHAR(n) same as NATIONAL CHARACTER(n) - Fixed width and " " padded character strings with UTF8 character set.

► VARCHAR(n) same as CHARACTER VARYING(n) - Variable width character strings. Default character set is ASCII.

► NVARCHAR(n) same as NATIONAL CHARACTER VARYING(n) - Variable width character strings with UTF8 character set.

► BINARY(n) - Fixed width and 0x00 padded byte strings.

► VARBINARY(n) same as BINARY VARYING(n) - Variable width byte string.

► TINYBLOB - BLOB (Binary Large Object) upto 255 bytes.

► BLOB - BLOB (Binary Large Object) upto 64K bytes.

► MEDIUMBLOB - BLOB (Binary Large Object) upto 16M bytes.

► LONGBLOB - BLOB (Binary Large Object) upto 4G bytes.

► TINYTEXT - CLOB (Binary Large Object) upto 255 characters.

► TEXT - CLOB (Binary Large Object) upto 64K characters.

► MEDIUMTEXT - CLOB (Binary Large Object) upto 16M characters.

► LONGTEXT - CLOB (Binary Large Object) upto 4G characters.

► ENUM - An enumeration to hold one entry of some pre-defined strings.

► SET - A set to hold zero or more entries of some pre-defined strings.

 185 views

70⟩ How To Convert Numeric Values to Character Strings?

You can convert numeric values to character strings by using the CAST(value AS CHAR) function as shown in the following examples:

SELECT CAST(4123.45700 AS CHAR) FROM DUAL;

4123.45700

-- How to get rid of the last 2 '0's?

SELECT CAST(4.12345700E+3 AS CHAR) FROM DUAL;

4123.457

SELECT CAST(1/3 AS CHAR);

0.3333

-- Very poor conversion

 203 views

71⟩ How To Enter Boolean Values in SQL Statements?

If you want to enter Boolean values in SQL statements, you use (TRUE), (FALSE), (true), or (false). Here are some good examples:

SELECT TRUE, true, FALSE, false FROM DUAL;

+------+------+-------+-------+

| TRUE | TRUE | FALSE | FALSE |

+------+------+-------+-------+

| 1 | 1 | 0 | 0 |

+------+------+-------+-------+

 205 views

72⟩ How To Convert Character Strings to Numeric Values?

You can convert character strings to numeric values by using the CAST(string AS DECIMAL) or CAST(string AS SIGNED INTEGER) function as shown in the following examples:

SELECT CAST('4123.45700' AS DECIMAL) FROM DUAL;

4123.46

-- Very poor conversion

SELECT CAST('4.12345700e+3' AS DECIMAL) FROM DUAL;

4123.46

-- Very poor conversion

SELECT CAST('4123.45700' AS SIGNED INTEGER) FROM DUAL;

4123

SELECT CAST('4.12345700e+3' AS SIGNED INTEGER) FROM DUAL;

4

-- Very poor conversion

 197 views

73⟩ How To Use LIKE Conditions?

A LIKE condition is also called pattern patch. There are 3 main rules on using LIKE condition:

* '_' is used in the pattern to match any one character.

* '%' is used in the pattern to match any zero or more characters.

* ESCAPE clause is used to provide the escape character in the pattern.

The following tutorial exercise provides you some good pattern matching examples:

SELECT 'GlobalGuideLine.com' LIKE '%center%' FROM DUAL;

1

SELECT 'GlobalGuideLine.com' LIKE '%CENTER%' FROM DUAL;

1

-- Case insensitive by default

SELECT 'GlobalGuideLine.com' LIKE '%CENTER_com' FROM DUAL;

1

 192 views

74⟩ How Many Groups of Data Types?

MySQL support 3 groups of data types as listed below:

► String Data Types - CHAR, NCHAR, VARCHAR, NVARCHAR, BINARY, VARBINARY, TINYBLOB, TINYTEXT, BLOB, TEXT, MEDIUMBLOB, MEDIUMTEXT, LONGBLOB, LONGTEXT, ENUM, SET

► Numeric Data Types - BIT, TINYINT, BOOLEAN, SMALLINT, MEDIUMINT, INTEGER, BIGINT, FLOAT, DOUBLE, REAL, DECIMAL.

► Date and Time Data Types - DATE, DATETIME, TIMESTAMP, TIME, YEAR.

 204 views

75⟩ How To Use Regular Expression in Pattern Match Conditions?

If you have a pattern that is too complex for LIKE to handle, you can use the regular expression pattern condition: REGEXP. The following tutorial exercise provides you some good examples:

SELECT 'GlobalGuideLine.com' REGEXP '.*ggl.*' FROM DUAL;

1

SELECT 'GlobalGuideLine.com' REGEXP '.*com$' FROM DUAL;

1

SELECT 'GlobalGuideLine.com' REGEXP '^F.*' FROM DUAL;

1

 181 views

76⟩ What Happens If NULL Values Are Involved in Expressions?

If NULL values are used in expressions, the resulting values will be NULL values. In other words:

► Arithmetic expressions with NULL values result NULL values.

► Comparison expressions with NULL values result NULL values.

► Logical expressions with NULL values result NULL values.

The tutorial exercise shows you some interesting examples:

SELECT NULL + NULL FROM DUAL;

NULL

SELECT NULL + 7 FROM DUAL;

NULL

SELECT NULL * 7 FROM DUAL;

NULL

SELECT NULL = NULL FROM DUAL;

NULL

SELECT 0 < NULL FROM DUAL;

NULL

SELECT '' > NULL FROM DUAL;

NULL

SELECT NULL AND TRUE FROM DUAL;

NULL

SELECT NULL OR TRUE FROM DUAL;

1

-- This is contradicting against the rules!

 187 views

77⟩ How To Use IN Conditions?

An IN condition is single value again a list of values. It returns TRUE, if the specified value is in the list. Otherwise, it returns FALSE.

Some examples are given in the tutorial exercise below:

SELECT 3 IN (1,2,3,4,5) FROM DUAL;

1

SELECT 3 NOT IN (1,2,3,4,5) FROM DUAL;

0

SELECT 'Y' IN ('F','Y','I') FROM DUAL;

1

 167 views

78⟩ How To Use CASE Expression?

There are 2 ways to use the CASE expression. The first way is to return one of the predefined values based on the comparison of a given value to a list of target values. The second way is to return one of the predefined values based on a list of conditions. Here is the syntax of both types of CASE expressions:

CASE value WHEN target_value THEN result

WHEN target_value THEN result

WHEN target_value THEN result

...

ELSE result

END

CASE WHEN condition THEN result

WHEN condition THEN result

WHEN condition THEN result

...

ELSE result

END

The tutorial exercise below gives two good examples:

SELECT CASE 'Sun' WHEN 'Mon' THEN 'Open'

WHEN "Fri" THEN "Open" ELSE 'Closed' END FROM DUAL;

Closed

SELECT CASE WHEN HOUR(CURRENT_TIME())<9 THEN 'Closed'

WHEN HOUR(CURRENT_TIME())>17 THEN 'Closed'

ELSE 'Open' END FROM DUAL;

Closed

 191 views

79⟩ How To Enter Microseconds in SQL Statements?

If you want to enter microseconds in a SQL statements, you can enter them right after the time string as a 6-digit number delimited with '.'. '0' will be padded to right if not enough digits. Here are some good examples:

SELECT TIME('1997/01/31 09^26^50.123') FROM DUAL;

09:26:50.123000

SELECT TIME('1997/01/31 09^26^50.000123') FROM DUAL;

09:26:50.000123

 178 views

80⟩ What Are Date and Time Data Types in MySQL?

MySQL supports the following date and time data types:

► DATE - A date in the range of '1000-01-01' and '9999-12-31'. Default DATE format is "YYYY-MM-DD".

► DATETIME - A date with the time of day in the range of '1000-01-01 00:00:00' and '9999-12-31 23:59:59'. Default DATETIME format is "YYYY-MM-DD HH:MM:SS".

► TIMESTAMP - A timestamp. The range is '1970-01-01 00:00:00' to partway through the year 2037. Default DATETIME format is "YYYY-MM-DD HH:MM:SS".

► TIME - A time. The range is '-838:59:59' to '838:59:59'. Default TIME format is "HH:MM:SS".

► TIME - A time. The range is '-838:59:59' to '838:59:59'. Default TIME format is "HH:MM:SS".

► YEAR - A year in 4 digits in the range of 1901 and 2155. Default YEAR format is "YYYY".

 212 views