Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to fetch data from database with SQL query but giving null records

My query is not working properly. I have an Employee table in my database with columns eid, ename, eaddress, and address has values Nagpur, Pune, Mumbai, Hyderabad, Delhi. I have to find the employees with a first name starting with "n", "m", "d".

SELECT * 
FROM student_records 
WHERE First_Name LIKE '\[nmd\]%';

I was trying to use this query, but it is returning only null.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Based on the information you provided, it looks like you’re trying to use the like keyword in your SQL query to filter the records by the first letter of the First_Name column. However, the syntax you’re using is incorrect.

The like keyword is used to match a pattern in a string. You can use wildcards, such as % and _, in the pattern to match any number of characters or a single character, respectively.

In your query, the pattern ‘[nmd]%’ matches any string that starts with one of the letters n, m, or d, followed by any number of characters. However, you’re using square brackets [] to enclose the list of letters, which is incorrect.

Square brackets are used in a like pattern to match any single character within the brackets. For example, ‘[abc]’ would match any string that contains the letter a, b, or c, but not strings that start with those letters.

To fix your query, you can use the or keyword to combine multiple like conditions, each with a different pattern that matches the starting letter you want. Here’s an example of how you could modify your query to do this:

Select * 
From student_records 
Where First_Name like 'n%' 
   or First_Name like 'm%' 
   or First_Name like 'd%';

This query will return all records where the First_Name column starts with n, m, or d, respectively.

Alternatively, you can use the in keyword to specify a list of values that you want to match. Here’s an example of how you could do this:

Select * 
From student_records 
Where First_Name like 'n%' 
   or First_Name like 'm%' 
   or First_Name like 'd%';

This query will return the same results as the previous example.

I hope this helps! Let me know if you have any other questions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading