Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
Keywords in Solidity
Next article icon

SQL FROM keyword

Last Updated : 27 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The SQL FROM keyword is a crucial component of SQL queries, used to specify the table from which data should be selected or manipulated. It plays a vital role in SELECT, DELETE, UPDATE and other DML operations, allowing users to interact with databases effectively. Understanding the FROM keyword is good for anyone working with SQL databases as it is the foundation for querying and modifying data.

In this article, We will learn about What is the SQL FROM Keyword along with various SQL FROM Keyword Examples and so on.

What is the SQL FROM Keyword?

  • The FROM keyword is used in SQL to specify the table from which data should be selected or deleted.
  • It is an essential component of SQL statements, particularly SELECT, DELETE, UPDATE and some other DML (Data Manipulation Language) operations.

Basic Syntax:

The basic syntax of the FROM keyword in a SELECT the statement is as follows:

SELECT column1, column2, ...
FROM table_name;

Explanation:

  • column1, column2, ... are the columns you want to retrieve.
  • table_name is the name of the table from which to retrieve the data.

SQL FROM Keyword Example

Lets understand SQL From with examples. Let's say we have a some tables called students, courses and enrollments as shown below:

-- Creating the database
CREATE DATABASE school;

-- Using the newly created database
USE school;

-- Creating the students table
CREATE TABLE students (
student_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
age INT,
birth_date DATE
);

-- Inserting sample data into the 'students' table
INSERT INTO students (name, age, birth_date) VALUES
('John', 20, '2001-05-10'),
('Alice', 18, '2003-02-15'),
('Bob', 22, '1999-10-20');

-- Creating the courses table
CREATE TABLE courses (
course_id INT PRIMARY KEY AUTO_INCREMENT,
course_name VARCHAR(100)
);

-- Inserting sample data into the 'courses' table
INSERT INTO courses (course_name) VALUES
('Mathematics'),
('Physics'),
('Chemistry');

-- Creating the enrollments table to associate students with courses
CREATE TABLE enrollments (
enrollment_id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

-- Inserting sample data into the 'enrollments' table
INSERT INTO enrollments (student_id, course_id) VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 3);

Courses Table

| student_id | name  | age | birth_date |
|------------|-------|-----|------------|
| 1 | John | 20 | 2001-05-10 |
| 2 | Alice | 18 | 2003-02-15 |
| 3 | Bob | 22 | 1999-10-20 |

Enrollments Table

| course_id | course_name |
|-----------|-------------|
| 1 | Mathematics |
| 2 | Physics |
| 3 | Chemistry |

Students Table

| enrollment_id | student_id | course_id |
|---------------|------------|-----------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 3 | 3 |

Query 1. Selecting Data from a Single Table

To retrieve data from a single table (employees), we use the FROM keyword to specify the table name. Here is an example:

SELECT name, age
FROM students;

Output:

| name  | age |
|-------|-----|
| John | 20 |
| Alice | 18 |
| Bob | 22 |

Query 2. Selecting Data from Multiple Tables

The FROM keyword can also be used to select data from multiple tables, often in conjunction with joins. Here is an example using an INNER JOIN:

SELECT students.name, courses.course_name
FROM students
INNER JOIN enrollments ON students.student_id = enrollments.student_id
INNER JOIN courses ON enrollments.course_id = courses.course_id;

Output:

| name  | course_name |
|-------|-------------|
| John | Mathematics |
| John | Physics |
| Alice | Mathematics |
| Bob | Chemistry |

Query 3. Subqueries

The FROM keyword can also be used with subqueries to create more complex queries. A subquery is a query nested within another query. Here is an example:

SELECT name
FROM (SELECT name, age FROM students WHERE age > 18) AS adult_students;

Output:

| name  |
|-------|
| John |
| Bob |

Query 4. Using FROM with DELETE and UPDATE

The FROM keyword is not only used with SELECT statements but also with DELETE and UPDATE statements to specify the table to be modified.

DELETE Example:

DELETE FROM students
WHERE age < 18;

Output:

| student_id | name  | age | birth_date |
|------------|-------|-----|------------|
| 1 | John | 20 | 2001-05-10 |
| 3 | Bob | 22 | 1999-10-20 |

This query deletes records from the students table where the age is less than 18.

UPDATE Example:

UPDATE students
SET age = age + 1
FROM students
WHERE birth_date = '2000-01-01';

Output:

| student_id | name  | age | birth_date |
|------------|-------|-----|------------|
| 1 | John | 20 | 2001-05-10 |
| 2 | Alice | 18 | 2003-02-15 |
| 3 | Bob | 22 | 1999-10-20 |

Important Point About SQL From Keyword

  1. When working with multiple tables, using aliases makes your SQL statements easier to read and maintain.
  2. Instead of using SELECT *, specify the columns you need. This can improve performance and readability.
  3. Use the appropriate type of join based on your requirement to ensure you get the correct results.
  4. Use the WHERE clause to filter data as early as possible to reduce the amount of data processed by subsequent operations.

Conclusion

In conclusion, the SQL FROM keyword is a fundamental aspect of SQL queries, enabling users to specify the source table for data operations. Whether selecting data from a single table, joining multiple tables, or using subqueries, the FROM keyword is essential for building powerful and efficient SQL statements.


Next Article
Keywords in Solidity
author
sumitmehra720661
Improve
Article Tags :
  • SQL
  • Databases

Similar Reads

  • Lateral Keyword in SQL
    The lateral keyword represents a lateral join between two or more tables. It joins the output of the outer query with the production of the underlying lateral subquery. It is like a for-each loop in SQL where the subquery iterates through each row of the concerned table, evaluating the subquery for
    3 min read
  • R - Keywords
    R is an open-source programming language that is widely used as a statistical software and data analysis tool. R generally comes with the Command-line interface. R is available across widely used platforms like Windows, Linux, and macOS. Also, the R programming language is the latest cutting-edge to
    6 min read
  • Keywords in Solidity
    Solidity keywords are words that are specifically reserved for their intended usage in the Solidity programming language and cannot be used elsewhere, such as variable or function names. To create smart contracts on Ethereum and other blockchain platforms, these terms are fundamental building blocks
    6 min read
  • SQL OR Operator
    The SQL OR operator is a powerful tool used to filter records in a database by combining multiple conditions in the WHERE clause. When using OR, a record will be returned if any of the conditions connected by the operator are true. This allows for more flexibility in querying and is important when w
    7 min read
  • SQL Operators
    SQL operators are important in database management systems (DBMS) as they allow us to manipulate and retrieve data efficiently. Operators in SQL perform arithmetic, logical, comparison, bitwise, and other operations to work with database values. Understanding SQL operators is crucial for performing
    6 min read
  • Long Tail Keywords in SEO
    Long tail keywords are specific and highly targeted search phrases consisting of three or more words that users enter into search engines. These keywords are characterized by their detailed and niche nature, addressing more specific queries or topics. The term "long-tail" is derived from the shape o
    11 min read
  • MySQL Full Form
    The full form of MySQL is My Structured Query Language. The name itself is a combination of "My," i.e. the name of co-founder Michael Widenius's daughter, and "SQL," the abbreviation for Structured Query Language. SQL is a standardized language used to manage and manipulate relational databases. MyS
    3 min read
  • SQL Comments
    SQL comments play an essential role in enhancing the readability, maintainability, and documentation of our SQL code. By using comments effectively, developers can provide context, clarify complex logic, and temporarily disable parts of the code. Whether we're working alone or collaborating with a t
    4 min read
  • SQL Query Complexity
    Structured Query Language (SQL) is the foundation for interacting with relational databases, making it an essential skill for developers, data analysts, and database administrators. In relational databases, data is stored in tables consisting of rows and columns, where each column holds data of a sp
    3 min read
  • SQL AND Operator
    In SQL, the AND operator is an essential tool used to combine multiple conditions in a WHERE clause. This allows us to filter records based on multiple criteria, making our queries more specific and tailored to our needs. When used correctly, the AND operator can help us retrieve data that satisfies
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences