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

SQL notes

The document provides SQL examples for creating and managing tables, including 'AllStudents', 'Students', 'Courses', and 'Employee'. It covers data types, inserting, deleting, selecting, and updating data with various SQL commands. Additionally, it illustrates the use of primary keys and efficient data manipulation techniques.

Uploaded by

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

SQL notes

The document provides SQL examples for creating and managing tables, including 'AllStudents', 'Students', 'Courses', and 'Employee'. It covers data types, inserting, deleting, selecting, and updating data with various SQL commands. Additionally, it illustrates the use of primary keys and efficient data manipulation techniques.

Uploaded by

moksh.goenka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL notes

SQL example to begin with:

CREATE TABLE AllStudents (

StudentName VARCHAR(100),

RollNumber CHAR(3),

Age INT,

PhoneNumber CHAR(10),

HomeRoomID CHAR(3)

);

INSERT INTO AllStudents (StudentName, RollNumber, Age, PhoneNumber, HomeRoomID)

VALUES

('John Doe', '001', 10, '1234567890', '101'),

('Jane Smith', '002', 11, '2345678901', '102'),

('Mark Johnson', '003', 12, '3456789012', '103'),

('Emily Williams', '004', 13, '4567890123', '104'),

('Chris Brown', '005', 14, '5678901234', '105'),

('Megan Davis', '006', 15, '6789012345', '106'),

('David Wilson', '007', 16, '7890123456', '107'),

('Emma Miller', '008', 17, '8901234567', '108'),

('Ryan Anderson', '009', 18, '9012345678', '109'),

('Sophia Taylor', '010', 16, '9123456789', '110');

SELECT * FROM AllStudents;

SELECT * FROM AllStudents

WHERE Age > 15;

DELETE FROM AllStudents

WHERE RollNumber = '003';


SQL query examples and information:

1: Introduction to DBMS and Creating a Table


-- Example 1: Creating a Students table
CREATE TABLE Students (
StudentID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
PRIMARY KEY (StudentID)
);

-- Example 2: Creating a Courses table


CREATE TABLE Courses (
CourseID INT,
CourseName VARCHAR(100),
Instructor VARCHAR(50),
PRIMARY KEY (CourseID)
);

2: Data Types and Initialization


Common SQL data types along with their sizes and use cases.
-- Common Data Types
-- INT: Integer values
-- VARCHAR(size): Variable-length character strings
-- DATE: Date values

-- Example: Creating a table with different data types


CREATE TABLE Employee (
EmployeeID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
BirthDate DATE,
Salary DECIMAL(10, 2),
PRIMARY KEY (EmployeeID)
);

-- Example: Inserting data into the Employee table


INSERT INTO Employee (EmployeeID, FirstName, LastName, BirthDate, Salary)
VALUES (1, 'John', 'Doe', '1990-05-15', 50000.00),
(2, 'Jane', 'Smith', '1985-08-22', 60000.50);

3: Primary Key
Illustrating the addition of a primary key during table creation and afterward using ALTER
TABLE.
-- Adding primary key with CREATE TABLE
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT
);

-- Adding primary key with ALTER TABLE


ALTER TABLE Courses
ADD PRIMARY KEY (CourseID);

4: Inserting Data with INSERT


This is how we can add multiple VALUES in a single query for efficiency.
-- Inserting multiple values in one query
INSERT INTO Students (StudentID, FirstName, LastName, Age)
VALUES
(3, 'Mark', 'Johnson', 12),
(4, 'Emily', 'Williams', 13),
(5, 'Chris', 'Brown', 14);

5: Removing Data
-- Deleting a specific student
DELETE FROM Students
WHERE StudentID = 2;

-- Deleting students older than 15


DELETE FROM Students
WHERE Age > 15;

6: Viewing Data with SELECT


Demonstrating various ways of using SELECT, including selecting specific columns.

-- Selecting all columns for students aged 12


SELECT *
FROM Students
WHERE Age = 12;

-- Selecting specific columns for all students


SELECT StudentID, FirstName
FROM Students;
7: Updating Data with UPDATE
-- Updating the age of a specific student
UPDATE Students
SET Age = 15
WHERE StudentID = 1;

-- Giving a salary raise to all employees


UPDATE Employee
SET Salary = Salary * 1.1;

You might also like