Basic SELECT statements and filtering (WHERE, LIKE, IN)
Basic SELECT Statements and Filtering (WHERE, LIKE, IN) A SELECT statement allows you to retrieve specific data from a database. The syntax for a SELECT...
Basic SELECT Statements and Filtering (WHERE, LIKE, IN) A SELECT statement allows you to retrieve specific data from a database. The syntax for a SELECT...
Basic SELECT Statements and Filtering (WHERE, LIKE, IN)
A SELECT statement allows you to retrieve specific data from a database. The syntax for a SELECT statement is:
sql
SELECT column_name1, column_name2, ...
FROM table_name
WHERE condition;
Columns:
The SELECT statement retrieves the specified columns from the specified table.
Table Name:
The table containing the data you want to retrieve.
WHERE Clause:
The WHERE clause filters the data based on specific conditions. It uses keywords like WHERE, AND, and OR to combine multiple conditions.
Examples:
sql
SELECT name, age FROM users WHERE age = 25;
sql
SELECT product_name, price
FROM orders
WHERE category = 'Electronics'
AND price > 100;
sql
SELECT name, city
FROM students
WHERE country IN ('USA', 'Canada');
Filtering with LIKE:
The LIKE operator is used for pattern matching. It allows you to search for data that matches a specified pattern.
Examples:
sql
SELECT name
FROM users
WHERE name LIKE '%J%';
sql
SELECT *
FROM products
WHERE product_name LIKE '%Apple%';
Filtering with IN:
The IN operator is used to search for data that is present in a specified list.
Examples:
sql
SELECT *
FROM orders
WHERE country IN ('UK');
sql
SELECT *
FROM employees
WHERE department IN ('IT', 'Marketing');
Additional Notes:
The SELECT statement can be used with multiple tables by joining them with the JOIN keyword.
The WHERE clause can also be used with subqueries to filter data recursively.
The LIKE operator is case-sensitive, while the IN operator is case-insensitive