sharing some Interview questions #sql #intermediate
1. How to select random rows from a table?
-> Using the RAND() function in combination with ORDER BY and LIMIT. In Mysql we use RAND() which returns random records from table.
SELECT * FROM table_name
ORDER BY RAND()
LIMIT 5;
2. How to find the last id in a table?
-> By using MAX(), We use following syntax
SELECT id
FROM table_name
ORDER BY id DESC
LIMIT 1;
3. How to find the values in a text column of a table that start with a certain letter?
-> Using the LIKE operator in combination with the % and _ wildcards. For example, we need to find all surnames in a table that start with "A". The query is:
SELECT * FROM table_name
WHERE surname LIKE 'A_';
1. How to select random rows from a table?
-> Using the RAND() function in combination with ORDER BY and LIMIT. In Mysql we use RAND() which returns random records from table.
SELECT * FROM table_name
ORDER BY RAND()
LIMIT 5;
2. How to find the last id in a table?
-> By using MAX(), We use following syntax
SELECT id
FROM table_name
ORDER BY id DESC
LIMIT 1;
3. How to find the values in a text column of a table that start with a certain letter?
-> Using the LIKE operator in combination with the % and _ wildcards. For example, we need to find all surnames in a table that start with "A". The query is:
SELECT * FROM table_name
WHERE surname LIKE 'A_';