SQL Querying Basics ๐
SQL querying is the act of retrieving data from a relational database using the
SELECT statement. The most basic form is SELECT column1, column2 FROM table WHERE condition ORDER BY column LIMIT n; โ and every variation (filtering, joining, grouping,
sorting, paginating) extends from this skeleton. SQL is declarative: you describe
what you want, not how to get it, and the database engine (Oracle, MySQL, PostgreSQL,
SQL Server, SQLite) figures out the optimal execution plan. Mastering SELECT is the
single most-used skill in any data-adjacent role (analyst, developer, data scientist,
DBA).
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โ
๐ป Implementations (Dialect Comparison)โ
- Oracle SQL
- MySQL
- PostgreSQL
-- ๐ 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โ
- 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: 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