0% found this document useful (0 votes)
11 views

Simple SQL Queries

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Simple SQL Queries

Uploaded by

Ayush Sinha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Simple SQL Queries

Here are some basic SQL queries that can be used to perform common database operations like
retrieving, filtering, and sorting data. These examples work with a simple table of students and
courses.

1. Selecting All Data (SELECT *)

This query retrieves all rows and columns from a table. It’s the most basic query and is used when
you need to view all the data in the table.

Example:

Consider the table `Students`:

StudentID Name Age Course Grade


101 Alice 22 Computer Science A
102 Bob 23 Engineering B
103 Charlie 21 Mathematics A

Query:

SELECT * FROM Students;

Result:

StudentID Name Age Course Grade


101 Alice 22 Computer Science A
102 Bob 23 Engineering B
103 Charlie 21 Mathematics A

2. Selecting Specific Columns (`SELECT column1, column2`)

When you want to view only specific columns, you can use the projection feature of SQL to retrieve
only the desired columns from a table.

Example:

Query: Retrieve the `Name` and `Course` of students from the `Students` table.

SELECT Name, Course FROM Students;

Result:

Name Course
Alice Computer Science
Bob Engineering
Charlie Mathematics
3. Filtering Rows (`WHERE` clause)

The `WHERE` clause is used to filter rows based on a condition. You can apply this to retrieve only
those rows that meet certain criteria.

Example:

Query: Retrieve the details of students who are studying "Engineering."

SELECT * FROM Students WHERE Course = 'Engineering';

Result:

StudentID Name Age Course Grade


102 Bob 23 Engineering B

4. Sorting the Results (`ORDER BY`)

You can use the `ORDER BY` clause to sort the data in ascending (`ASC`) or descending (`DESC`) order
based on one or more columns.

Example:

Query: Retrieve the student details and sort them by `Name` in ascending order

SELECT * FROM Students ORDER BY Name ASC;

Result:

StudentID Name Age Course Grade


101 Alice 22 Computer Science A
102 Bob 23 Engineering B
103 Charlie 21 Mathematics A

5. Using `LIMIT` to Retrieve a Certain Number of Rows

If you want to retrieve a specific number of rows from the result set, you can use the `LIMIT` clause.

Example:

Query: Retrieve the first two rows from the `Students` table.

SELECT * FROM Students LIMIT 2;

Result:

StudentID Name Age Course Grade


101 Alice 22 Computer Science A
102 Bob 23 Engineering B
6. Pattern Matching (`LIKE`)

The `LIKE` operator is used to search for a specific pattern in a column. You can use `%` as a wildcard
to match any sequence of characters.

Example:

Query: Find all students whose names start with 'A'.

SELECT * FROM Students WHERE Name LIKE 'A%';

Result:

StudentID Name Age Course Grade


101 Alice 22 Computer Science A

7. Aggregating Data (`COUNT`, `SUM`, `AVG`)

You can use SQL aggregate functions like `COUNT`, `SUM`, and `AVG` to perform calculations on
columns.

Example 1: Count the number of students in the `Students` table.

SELECT COUNT(*) FROM Students;

Result:

Count
3

Example 2: Calculate the average age of students.

SELECT AVG(Age) AS AverageAge FROM Students;

Result:

AverageAge
22.00

8. Grouping Data (`GROUP BY`)

The `GROUP BY` clause is used with aggregate functions to group the result set by one or more
columns.

Example:

Query: Find the number of students in each course.

SELECT Course, COUNT(*) AS NumStudents FROM Students GROUP BY Course;


Result:

Course NumStudents
Computer Science 1
Engineering 1
Mathematics 1

These examples cover some of the fundamental SQL operations that you will frequently use when
interacting with databases. By understanding these basic queries, you’ll be able to retrieve, filter, and
manipulate data efficiently in a DBMS.

You might also like