Lesson 2: Introduction to SQL
Welcome to Lesson 2. In the last lesson, we learned that the database is like a "digital filing cabinet." Now, we are going to learn the language used to talk to that cabinet.
That language is SQL. In this Introduction to SQL for beginners, you will learn how to use SQL to fetch data from a database.
1. What is SQL?
SQL stands for Structured Query Language.
It is the standard language for storing, manipulating, and retrieving data in a relational database.
- You: "Hey database, show me the names of all employees who joined today."
- SQL:
SELECT emp_name FROM employees; - Database: "Here is the list."
Important Note: SQL is NOT a database (like Oracle or MySQL). SQL is the language used to talk to those databases.
2. The 3 Golden Rules of SQL Syntax
Just like English has grammar rules, SQL has syntax rules. If you break them, the database won't understand you.
| Rule | Explanation |
|---|---|
| 1. Semicolons (;) | In every SQL tool and real-world project, SQL statements should end with a semicolon. It tells the database where the command ends. |
| 2. Case Insensitive | SELECT is the same as select. However, we usually write keywords in UPPERCASE to make code readable. |
| 3. Quotes | Text data (like names) must be inside single quotes: 'John'. Numbers do not need quotes: 100. |
3. The "Hello World" of SQL: SELECT & FROM
In every programming language, the first thing you learn is how to print "Hello World." In SQL, our "Hello World" is the SELECT statement.
To fetch data, you need to answer two questions:
- SELECT: What columns do you want to see?
- FROM: Which table are they inside?
Scenario A: Selecting Specific Columns
Imagine we have a table called employees with columns: emp_id, emp_name, and hire_date.
If you only want to see the names of the employees:
SELECT emp_name
FROM employees;
Scenario B: Selecting All Columns (*)
If you want to see everything in the table (all columns), you use the asterisk symbol (*).
SELECT *
FROM employees;
⚠️ Warning: In real projects with millions of rows, avoid using SELECT * because it can slow down the system. Only ask for the columns you need!
4. Your Turn: Write Your First Query
You don't need to install anything. Let's practice right now.
Step 1: Click here to open OracleSQL or SQL Fiddle.
Step 2: In the Editor, copy and paste this SQL SETUP Code to create your table:
⚠️ Note: If you see an error saying the table already exists, refresh the page and run the setup again.
CREATE TABLE employees (
emp_id INT,
emp_name VARCHAR(50),
hire_date DATE
);
INSERT INTO employees VALUES
(1, 'John', '2024-01-01'),
(2, 'Alice', '2024-01-01');
Step 3: Now, copy the query below and run it to see the result:
FROM employees;
Step 4: Click "Run". You should see the data appear below!
Summary: Today we learned that SQL is a language, not a database. We learned that every query needs a SELECT (columns) and a FROM (table).
Next Lesson: In Lesson 3, we will look at Data Types—why some data is text, some is numbers, and some is dates.
Comments
Post a Comment