This is a premium alert message you can set from Layout! Get Now!

Basic SQL Syntax and Conventions

Basic SQL Syntax and Conventions

You've already written your first SQL query — now it’s time to understand how SQL actually works under the hood. In this chapter, we’ll break down the structuresyntax, and best practices of writing SQL queries like a pro. Think of this as learning the grammar of a new language — without the boring grammar classes. 😉

📐 SQL Syntax 101

SQL (Structured Query Language) has a very readable syntax. Here's a basic structure of a SQL statement:

SELECT column1, column2, ... FROM table_name WHERE condition ORDERBY column;

✅ Example:

Let’s say we want to get the names and marks of students from the students table:

SELECT name, marks FROM students;

🔍 Output:

name marks
Rahul 85
Priya 92
Aman 78
Sneha 88
Karan 95

🧱 SQL Statement Structure

Clause Purpose
SELECT Specifies the columns you want to see
FROM Specifies the table you're querying
WHERE Filters rows based on a condition (optional)
ORDER BY Sorts the result (optional)
GROUP BY Groups rows that have the same values (optional)
HAVING Filters groups after GROUP BY (optional)
LIMIT Limits the number of results (optional)

We'll go deep into each one in later chapters, so don't worry.

📝 SQL Syntax Rules (Must-Know Conventions)

Case Insensitive 

SQL keywords are not case sensitive:

SELECT * FROM students; select * from students; SeLeCt * FrOm students;

All do the same thing. But best practice is to write keywords in uppercase.

Statements End with Semicolon (;)

While some systems allow you to skip;, it's good practice to use it.

Single Quotes for Strings 

When comparing string values, always use single quotes:

WHERE name='Rahul';

Comments in SQL

Just like other languages, you can comment your code:

-- This is a single-line comment /* This is a multi-line comment
*/

⚠️ Common Mistakes to Avoid

Mistake Fix
Forgetting quotes around strings WHERE name = 'Aman'(✅) not WHERE name = Aman(❌)
Using=withNULL Use IS NULL instead of= NULL
Case sensitivity in data 'rahul''Rahul'in many DBMS
Misspelled column/table names Always double-check spelling!

🔍 Example Queries Using Our Dataset

1. Get names of students enrolled in SQL course:

SELECT name FROM students WHERE course='SQL';

2. List students with marks greater than 90:

SELECT name, marks FROM students WHERE marks > 90;

3. Show all student names in alphabetical order:

SELECT name FROM students ORDER BY name ASC;

📚 Summary

  • SQL is simple but powerful — clean syntax and natural language feel.
  • Stick to best practices like uppercase keywords and clean formatting.
  • Avoid common mistakes like missing quotes or misusing NULL.

Tags

Post a Comment

0 Comments
Post a Comment
To Top