Skip to content

SQL Querying Basics πŸš€ΒΆ

Prerequisites: None

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) β†’