SQL Queries

  • Learn how to write SQL queries to retrieve and filter data.
  • SELECT Statement

    What is SELECT?

    SELECT is used to retrieve data from a table.

    Basic Syntax:

Using SELECT Statement in SQL

The SELECT statement is used to retrieve data from a table. You can specify one or more columns to fetch data from the given table.

SELECT column1, column2
FROM table_name;
  • Example Table: students

    id

    name

    subject

    marks

    1

    Rahul

    Math

    85

    2

    Priya

    Science

    90

    3

    Amit

    Math

    75



    Example 1: Select All Columns

SELECT * FROM students;
  • * means all columns.


    Example 2: Select Specific Columns

SELECT name, marks FROM students;
  • Shows only name and marks.

    WHERE Clause

    What is WHERE?

    WHERE is used to filter records based on conditions.

    Basic Syntax:

Using WHERE Clause in SQL

The WHERE clause is used to filter records in a SQL query based on specific conditions, returning only the rows that meet the given criteria.

SELECT column_name
FROM table_name
WHERE condition;
  • Example 1: Marks Greater Than 80

Filtering Records with WHERE Condition

This SQL query retrieves all records from the students table where the marks are greater than 80 using the WHERE clause.

SELECT * FROM students
WHERE marks > 80;
  • Shows students who scored above 80.


    Example 2: Subject is Math

SELECT * FROM students
WHERE subject = 'Math';
  • Common Operators in WHERE

    Operator

    Meaning

    =

    Equal

    >

    Greater than

    <

    Less than

    >=

    Greater or equal

    <=

    Less or equal

    !=

    Not equal

    BETWEEN

    Range

    IN

    Multiple values

    LIKE

    Pattern matching


    Example 3: BETWEEN

Using BETWEEN in SQL

This SQL query retrieves all records from the students table where the marks are between 70 and 90 (inclusive) using the BETWEEN condition.

SELECT * FROM students
WHERE marks BETWEEN 70 AND 90;
  • Example 4: IN

Using IN Operator in SQL

This SQL query retrieves all records from the students table where the subject is either 'Math' or 'Science' using the IN operator.

SELECT * FROM students
WHERE subject IN ('Math', 'Science');
  • Example 5: LIKE

Using LIKE Operator in SQL

This SQL query retrieves all records from the students table where the name starts with the letter 'R' using the LIKE operator and wildcard %.

SELECT * FROM students
WHERE name LIKE 'R%';
  • Names starting with "R".