The FROM keyword in SQL is used to specify the source table(s) from which data will be retrieved in a query. It is a crucial part of the SELECT statement, which is used to query data from a database.
Syntax:
SELECT column1, column2, ...
FROM table_name;
Key Features of the FROM Clause:
-
Specify Single Table: Retrieves data from a single table.
SELECT name, age FROM employees; -
Join Multiple Tables: Combines data from two or more tables using JOIN operations.
SELECT employees.name, departments.department_name FROM employees JOIN departments ON employees.department_id = departments.id; -
Use Aliases: Assign a short name (alias) to a table for convenience.
SELECT e.name, d.department_name FROM employees AS e JOIN departments AS d ON e.department_id = d.id; -
Subqueries in
FROM: Include the result of a subquery as a derived table.SELECT sub.name, sub.total_sales FROM (SELECT name, SUM(sales) AS total_sales FROM sales GROUP BY name) AS sub; -
With Other Clauses: Works with
WHERE,GROUP BY,ORDER BY, and other clauses to refine or structure the query.SELECT name FROM employees WHERE age > 30 ORDER BY name ASC;
Best Practices:
- Use meaningful aliases for better readability when working with multiple tables.
- Ensure that tables and joins are optimized for performance.
- Be cautious with subqueries in the
FROMclause as they can impact query performance.
If you have a specific example or question about the FROM keyword, feel free to ask!
No comments:
Post a Comment