0% found this document useful (0 votes)
15 views2 pages

Essential SQL Commands and Examples

SQL (Structured Query Language) is a language for managing data in relational databases, with basic commands such as SELECT, INSERT, UPDATE, and DELETE. It includes functionalities for creating tables, inserting and selecting data, using aggregate functions, and performing joins and subqueries. Additional tips include using LIMIT for row restriction and DISTINCT to eliminate duplicates.

Uploaded by

gsidhvi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Essential SQL Commands and Examples

SQL (Structured Query Language) is a language for managing data in relational databases, with basic commands such as SELECT, INSERT, UPDATE, and DELETE. It includes functionalities for creating tables, inserting and selecting data, using aggregate functions, and performing joins and subqueries. Additional tips include using LIMIT for row restriction and DISTINCT to eliminate duplicates.

Uploaded by

gsidhvi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL Notes

What is SQL?

SQL (Structured Query Language) is used to store, retrieve, manage, and manipulate data in relational

databases.

Basic SQL Commands

SELECT - Retrieve data

INSERT - Add new data

UPDATE - Modify data

DELETE - Remove data

CREATE - Create table/database

DROP - Delete table/database

WHERE - Conditions

ORDER BY - Sorting

Create Table

CREATE TABLE Students (

ID INT PRIMARY KEY,

Name VARCHAR(100),

Age INT,

Grade CHAR(1)

);

Insert Data

INSERT INTO Students (ID, Name, Age, Grade)

VALUES (1, 'Alice', 20, 'A');

Select Data

SELECT * FROM Students;

SELECT Name, Age FROM Students WHERE Age > 18;


SQL Notes

Update Data

UPDATE Students SET Grade = 'B' WHERE ID = 1;

Delete Data

DELETE FROM Students WHERE Name = 'Alice';

Aggregate Functions

SELECT COUNT(*) FROM Students;

SELECT AVG(Age) FROM Students;

SELECT MAX(Age), MIN(Age) FROM Students;

Group By and Having

SELECT Grade, COUNT(*) FROM Students GROUP BY Grade HAVING COUNT(*) > 1;

Joins

SELECT [Link], [Link]

FROM Students

INNER JOIN Courses ON [Link] = [Link];

Subqueries

SELECT Name FROM Students WHERE Age > (SELECT AVG(Age) FROM Students);

Aliases

SELECT Name AS StudentName, Age AS StudentAge FROM Students;

Tips

- Use LIMIT to restrict rows

- Use DISTINCT to remove duplicates

- Use WHERE, AND, OR, NOT for filtering

Common questions

Powered by AI

'GROUP BY' is used to arrange identical data into groups, often combined with aggregate functions like COUNT, SUM, AVG. 'HAVING' filters data after grouping. For example, to find how many students received each grade, with only those grades appearing more than once: SELECT Grade, COUNT(*) FROM Students GROUP BY Grade HAVING COUNT(*) > 1; The 'GROUP BY' aggregates students by grades, while 'HAVING' filters out any group appearing only once .

The 'UPDATE' command modifies existing records in a database to reflect changes, crucial for maintaining data integrity. For instance, updating a student's grade: UPDATE Students SET Grade = 'B' WHERE ID = 1; This ensures data stay accurate and relevant. Proper constraints and verification steps must accompany updates to prevent inconsistent or erroneous data entries, reinforcing database integrity over time .

Aliases in SQL are used to provide a temporary name to a table or a column for the duration of a query, improving readability and clarity of the results. For instance, when retrieving student names and ages, one might use: SELECT Name AS StudentName, Age AS StudentAge FROM Students; This query assigns 'StudentName' and 'StudentAge' as temporary names, making the output more understandable especially when combining with multiple tables .

The 'WHERE' clause filters records that fulfill a specified condition, refining data retrieval. A common use case is fetching information about students above a certain age: SELECT Name, Age FROM Students WHERE Age > 18. This query returns names and ages of students older than 18, demonstrating conditional access to specific data subsets .

Subqueries are nested queries used within WHERE, SELECT, or FROM clauses of another SQL query to perform multi-step operations. An example is finding students older than the average age: SELECT Name FROM Students WHERE Age > (SELECT AVG(Age) FROM Students); The subquery calculates the average age, and the main query retrieves names of students whose age exceeds that average .

Aggregate functions perform calculations on a set of values to return a single summarizing value, enhancing queries by enabling data analysis. Examples include: SELECT COUNT(*) FROM Students; to count all students, SELECT AVG(Age) FROM Students; to calculate the average age, and SELECT MAX(Age), MIN(Age) FROM Students; to find the oldest and youngest student ages. These functions provide insights by crunching data efficiently in SQL .

The 'JOIN' operation in SQL allows combining rows from two or more tables based on a related column. For example, to retrieve student names along with their enrolled course names, one might use: SELECT Students.Name, Courses.CourseName FROM Students INNER JOIN Courses ON Students.ID = Courses.StudentID; This example uses the INNER JOIN to connect 'Students' and 'Courses' tables on the matching student ID, facilitating a relational link between student information and their courses .

The 'DELETE' command removes specified records from a table, which can lead to permanent data loss if not handled carefully. For example, DELETE FROM Students WHERE Name = 'Alice'; removes Alice's entire record. Precautions include ensuring correct WHERE conditions to avoid unintentional deletions, considering backup strategies, and possibly using transactions to allow rollback options if supported by the SQL environment, mitigating risk of accidental data removal .

The 'DISTINCT' keyword eliminates duplicate rows in the result set, ensuring that only unique values are returned. When you want to list unique grades awarded to students, use: SELECT DISTINCT Grade FROM Students; This ensures each grade appears only once in the result set, reducing redundancy and clarifying data distribution .

The 'LIMIT' clause is used to restrict the number of records returned by a query, making retrieval operations more efficient especially when dealing with large datasets. In student records, if you want to see just the first five entries from the table, you could use: SELECT * FROM Students LIMIT 5; This limits the results to the first five records, which is useful in situations like previewing a dataset .

You might also like