This beginner-friendly SQL lesson is designed for students who want to learn how to work with databases and build a strong foundation in SQL. No prior SQL experience are also eligible.
1. What is SQL?
SQL (Structured Query Language) is a language used to communicate with databases.
For example, a company may have an Employee table:
| Employee_ID | Name | Department | Salary |
|---|---|---|---|
| 101 | Rahul | IT | 60000 |
| 102 | Priya | HR | 50000 |
| 103 | Amit | IT | 75000 |
| 104 | Neha | Finance | 65000 |
Using SQL, we can ask questions such as:
- Who works in the IT department?
- What is Amit's salary?
- How many employees are there?
- Which employees earn more than ₹60,000?
2. Understanding the Basic SQL Structure
The simplest SQL query is:
SELECT column_name
FROM table_name; For example:
SELECT Name
FROM Employee; This returns the names of all employees.
To retrieve multiple columns:
SELECT Name, Department, Salary
FROM Employee; To retrieve all columns:
SELECT *
FROM Employee; Key Concept
Think of SQL as asking a question:
SELECT → What information do I want?
FROM → Where is that information stored?
3. Filtering Data Using WHERE
Suppose we only want employees from the IT department.
SELECT Name, Salary
FROM Employee
WHERE Department = 'IT'; Result:
| Name | Salary |
| Rahul | 60000 |
| Amit | 75000 |
The WHERE clause is used to filter records based on a condition.
Another Example
Find employees earning more than ₹60,000:
SELECT Name, Salary
FROM Employee
WHERE Salary > 60000; 4. Using Multiple Conditions
We can combine conditions using AND and OR.
AND
Find employees from IT earning more than ₹60,000:
SELECT Name, Salary
FROM Employee
WHERE Department = 'IT'
AND Salary > 60000; OR
Find employees who work in either IT or HR:
SELECT Name, Department
FROM Employee
WHERE Department = 'IT'
OR Department = 'HR'; 5. Practical Exercise
Try writing SQL queries for the following:
Exercise 1
Display the names and departments of all employees.
Exercise 2
Display employees who work in Finance.
Exercise 3
Display employees earning more than ₹60,000.
Exercise 4
Display employees from IT whose salary is greater than ₹60,000.
Exercise 5
Display the names and salaries of employees earning exactly ₹50,000.
6. Quick Recap
Today we learned:
- What SQL is
- What a database table looks like
SELECTFROMWHERE- Comparison operators such as
=,>, and< - Using
ANDandOR - How to write basic SQL queries
Next Lesson
In the next lesson, we can build on these concepts with ORDER BY, DISTINCT, aggregate functions such as COUNT and SUM, GROUP BY, and HAVING, followed by practical business/analytics problems.
0