Download Android App in your Android Device from Google Play Store
- Search for "Withoutbook Practice Exam Test" in Mobile/Tablet Play Store
Practice InterviewNew Search by Name or Email

Exams Attended

Make Homepage

Bookmark this page

Subscribe Email Address

MySQL Interview Questions and Answers

Ques 76. How To Use CASE Expression?

Ans. 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

Is it helpful? Add Comment View Comments
Ques 77. What Are Date and Time Data Types in MySQL?
Ans. 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".
Is it helpful? Add Comment View Comments
Ques 78. How To Write Date and Time Literals?
Ans. MySQL offers a number of formats for you to use to enter date and time literals:

► ANSI standard format: "YYYY-MM-DD HH:MM:SS".
► Non-standard limiters. Like: "YYYY/MM/DD HH^MM^SS" or "YYYY.MM.DD HH-MM-SS".
► No limiters. Like: "YYYYMMDD" for a date or "HHMMSS" for a time.
► Decimal numbers. Like: 8 digits dddddddd for a date or 6 digits dddddd for a time.

The tutorial exercise below gives you some good examples:

SELECT DATE('1997-01-31') FROM DUAL;
1997-01-31

SELECT DATE('1997-01-31 09:26:50') FROM DUAL;
1997-01-31

SELECT TIME('1997-01-31 09:26:50') FROM DUAL;
09:26:50

SELECT DATE('1997/01/31 09^26^50') FROM DUAL;
1997-01-31

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

SELECT DATE('19970131') FROM DUAL;
1997-01-31

SELECT TIME('092650') FROM DUAL;
09:26:50

SELECT DATE(19970131) FROM DUAL; -- Crazy format
1997-01-31

SELECT TIME(092650) FROM DUAL; -- Crazy format
09:26:50
Is it helpful? Add Comment View Comments
Ques 79. How To Enter Microseconds in SQL Statements?
Ans. 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
Is it helpful? Add Comment View Comments
Ques 80. How To Convert Dates to Character Strings?
Ans. You can convert dates to character strings using the DATE_FORMAT(date, format) function. MySQL supports the following basic formatting codes:


► %a Abbreviated weekday name (Sun..Sat)
► %b Abbreviated month name (Jan..Dec)
► %c Month, numeric (0..12)
► %D Day of the month with English suffix (0th, 1st, 2nd, 3rd,...)
► %d Day of the month, numeric (00..31)
► %e Day of the month, numeric (0..31)
► %f Microseconds (000000..999999)
► %H Hour (00..23)
► %h Hour (01..12)
► %I Hour (01..12)
► %i Minutes, numeric (00..59)
► %j Day of year (001..366)
► %k Hour (0..23)
► %l Hour (1..12)
► %M Month name (January..December)
► %m Month, numeric (00..12)
► %p AM or PM
► %r Time, 12-hour (hh:mm:ss followed by AM or PM)
► %S Seconds (00..59)
► %s Seconds (00..59)
► %T Time, 24-hour (hh:mm:ss)
► %W Weekday name (Sunday..Saturday)
► %w Day of the week (0=Sunday..6=Saturday)
► %Y Year, numeric, four digits
► %y Year, numeric (two digits)
Is it helpful? Add Comment View Comments

Most helpful rated by users:

©2020 WithoutBook