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
SELECTandFROMdo 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)ΒΆ
π§ Step-by-Step LogicΒΆ
- Start π
- Point to the Table (FROM clause).
- Pick the Columns (SELECT clause).
- Run the query.
- 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 tableDUAL(e.g.,SELECT 1+1 FROM DUAL;)!"
π‘ Pro Tip: "The goal is to turn data into information, and information into insight." - Carly Fiorina