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

sql commands

The document provides an overview of SQL commands including SELECT, WHERE, ORDER BY, GROUP BY, SUM, COUNT, AVG, and INNER JOIN, along with examples for each command. It explains how to filter, sort, group, and aggregate data from a 'students' table and how to join it with a 'classes' table. Additionally, it briefly mentions the UPDATE command for modifying data.

Uploaded by

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

sql commands

The document provides an overview of SQL commands including SELECT, WHERE, ORDER BY, GROUP BY, SUM, COUNT, AVG, and INNER JOIN, along with examples for each command. It explains how to filter, sort, group, and aggregate data from a 'students' table and how to join it with a 'classes' table. Additionally, it briefly mentions the UPDATE command for modifying data.

Uploaded by

HAK
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SELECT...

FROM,

This gets the name and age of all students from the
students table.
Select name from Students
Field name Table name
https://sqlfiddle.com/sql-server/online-compiler?id=bf6e7886-ba1c-
4961-9b6e-8eb4b9500682
WHERE
Filters records (only shows data that meets a condition).

• SELECT * FROM table_name WHERE condition;

• SELECT * FROM students WHERE age > 18;


• This shows all columns of students who are older than 18.

• https://sqlfiddle.com/sql-server/online-compiler?id=5ada5e81-99f8-
463e-ac47-c1b439b8d631
ORDER BY
Purpose: Sorts the result in ascending or descending order.

• SELECT * FROM students ORDER BY age ASC; -- ASC (ascending),


DESC (descending)

• SELECT name, age FROM students ORDER BY age DESC;


• This shows names and ages sorted from oldest to youngest.

• https://sqlfiddle.com/sql-server/online-compiler?id=412a84c9-630a-4
e06-8a89-5f5480e866a9
• https://sqlfiddle.com/sql-server/online-compiler?id=23cf6b42-6c77-
42af-bf8b-664b14894711
GROUP BY
Purpose: Groups rows with the
same values in a column.
SELECT column_name, aggregate_function(column_name)
FROM table_name
GROUP BY column_name;

SELECT class, COUNT(*) FROM students GROUP BY class;


This counts how many students are in each class.

https://sqlfiddle.com/sql-server/online-compiler?id=54472182-65ff-44b
e-b8f4-e0b4c5c6df79
SELECT Grade, COUNT(*) FROM students GROUP BY Grade;
SUM
Adds values in a numeric column.

SELECT SUM(marks) FROM students;


➡️This shows the total of all students' marks.
COUNT
Counts the number of records.

SELECT COUNT(*) FROM students WHERE age >


16;

➡️Counts students older than 16.


AVG
Calculates the average value of a numeric column.

SELECT AVG(marks) FROM students;

➡️This gives the average marks of all students.


INNER JOIN?

An INNER JOIN combines rows from two or more tables


based on a matching column between them.

It returns only the rows that have matching values


in both tables.
SELECT table1.column1, table2.column2
FROM table1
INNER JOIN table2
ON table1.common_column =
table2.common_column;
Student & Class Tables
🎯 Table 1: students

🎯 Table 2: classes

class_id class_name
101 Computer Sci
102 Maths
103 Physics

student_id name class_id


1 Alice 101
2 Bob 102
3 Clara 101
• SELECT students.name, classes.class_name
• FROM students
• INNER JOIN classes
• ON students.class_id = classes.class_id;
name class_name
Alice Computer Sci
Bob Maths
Clara Computer Sci

Notice:
•Student Clara is matched with class Computer Sci because her class_id is 101.
•The class with ID 103 (Physics) is not shown because no student belongs to it.
•This is what makes it an "INNER" join — it only returns matching records.
• The UPDATE command is used to change the data. If the band ABC recruited an
extra member , SQL would make the change needed.
• UPDATE Band
• SET NumberOfMembers = 6
• WHERE BandName= “ABC”

You might also like