Skip to content

SQL Querying Basics ๐Ÿš€

Mentor's Note: SQL is the "Universal Language" of data. Whether you use Oracle, MySQL, or Postgres, the basic way you ask for data is almost identical! ๐Ÿ’ก


๐ŸŒŸ The Scenario: The Digital Filing Cabinet ๐Ÿ—„๏ธ

Imagine you are the manager of a large school.

  • The Logic: You have a massive Filing Cabinet (The Database). Inside, you have a drawer labeled "Students" (The Table). ๐Ÿ“ฆ
  • The Action: You want to see only the Names and Roll Numbers of every student.
  • The Result: You pull out the drawer and look at only those two columns. This is exactly what SELECT and FROM do in SQL. โœ…

๐Ÿ“– Concept Explanation

1. The SELECT Statement

The SELECT statement is used to pick which columns (fields) you want to see. - Use SELECT * if you want to see ALL columns. ๐ŸŒŸ

2. The FROM Statement

The FROM statement tells the database which table (drawer) the data is in.

3. Column Aliases (AS)

Sometimes table columns have technical names like STU_NM. You can rename them in your result using AS to make them readable, like AS "Student Name".


๐ŸŽจ Visual Logic: The Extraction Flow

graph LR
    subgraph Table: ["STUDENTS ๐Ÿ“‹"]
    C1[ID]
    C2[NAME]
    C3[AGE]
    end
    C1 --- SELECT[SELECT ID, NAME]
    C2 --- SELECT
    SELECT --> Result["Result Set โœ…"]

๐Ÿ’ป Implementations (Dialect Comparison)

-- ๐Ÿ›’ Scenario: Getting a list of all students
-- ๐Ÿš€ Action: Extracting ID and Name columns

SELECT student_id, student_name AS "Name"
FROM students;

-- ๐Ÿ›๏ธ Outcome: A clean list of IDs and Names.
-- ๐Ÿ›’ Scenario: Same logic as Oracle
-- ๐Ÿš€ Action: Standard SELECT

SELECT student_id, student_name AS Name
FROM students;

-- ๐Ÿ›๏ธ Outcome: Identical to Oracle.
-- ๐Ÿ›’ Scenario: Same logic as Oracle
-- ๐Ÿš€ Action: Standard SELECT

SELECT student_id, student_name AS "Name"
FROM students;

-- ๐Ÿ›๏ธ Outcome: Identical to Oracle.

๐Ÿง  Step-by-Step Logic

  1. Start ๐Ÿ
  2. Point to the Table (FROM clause).
  3. Pick the Columns (SELECT clause).
  4. Run the query.
  5. End ๐Ÿ

๐Ÿ“Š Sample Dry Run

Step Instruction Action Result
1 FROM employees Open Employees table ๐Ÿ—„๏ธ Table ready.
2 SELECT salary Hide all columns except salary ๐Ÿ™ˆ Salary list.

๐Ÿ“‰ Technical Analysis

  • Performance: Selecting specific columns is much faster than SELECT * because the database transfers less data over the network. ๐ŸŽ๏ธ

๐ŸŽฏ Practice Lab ๐Ÿงช

Task: The Price List

Task: You have a table named PRODUCTS. Write a query to show only the PRODUCT_NAME and PRICE. Hint: SELECT name, price FROM products; ๐Ÿ’ก


๐Ÿ’ก Interview Tip ๐Ÿ‘”

"Interviewers often ask: 'Can we use SELECT without FROM?' Answer: In MySQL/Postgres, Yes (e.g., SELECT 1+1;). In Oracle, Noโ€”you must use the dummy table DUAL (e.g., SELECT 1+1 FROM DUAL;)!"


๐Ÿ’ก Pro Tip: "The goal is to turn data into information, and information into insight." - Carly Fiorina


Next: Filtering Data (WHERE) โ†’