Retrieving specific data using SELECT statements

SELECT * FROM employees WHERE department = 'IT';

This query will return all rows where the department is 'IT'.

Using comparison operators in WHERE clause

You can use various comparison operators in the WHERE clause to filter the data more precisely. Common comparison operators are:

  • = (Equal)

  • != or <> (Not equal)

  • < (Less than)

  • > (Greater than)

  • <= (Less than or equal to)

  • >= (Greater than or equal to)

For example, you can retrieve all employees with a salary greater than 50000 using the following query:

SELECT * FROM employees WHERE salary > 50000;

Using logical operators in WHERE clause

To further refine your data retrieval, you can use logical operators in the WHERE clause. The logical operators are:

  • AND: Both conditions must be true for the row to be included in the result set.

  • OR: At least one of the conditions must be true for the row to be included in the result set.

  • NOT: The condition must be false for the row to be included in the result set.

For example, you can retrieve all employees who have a salary greater than 50000 and work in the 'IT' department using the following query:

SELECT * FROM employees WHERE salary > 50000 AND department = 'IT';

Using the LIKE operator to search for specific patterns

Sometimes, you need to search for specific patterns in the data. In such cases, you can use the LIKE operator in the WHERE clause. The LIKE operator allows you to use wildcard characters to match different patterns:

  • %: Represents zero, one, or multiple characters.

  • _: Represents a single character.

For example, you can retrieve all employees whose first name starts with 'J' using the following query:

SELECT * FROM employees WHERE first_name LIKE 'J%';

Similarly, you can retrieve all employees whose last name has exactly three characters using the following query:

SELECT * FROM employees WHERE last_name LIKE '___';

In conclusion, using SQL SELECT statements along with the WHERE clause, comparison operators, logical operators, and the LIKE operator allows you to effectively retrieve specific data from a database. By mastering these techniques, you'll be able to efficiently manage and analyze your data in a Database Management System.

Last updated