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

Day_2_SQL_Notes

The document provides an overview of SQL data modification commands including INSERT, UPDATE, and DELETE, along with their syntax and examples. It also covers basic SQL functions such as string, numeric, and date functions, with exercises for practice. Additionally, it includes practice queries for creating and manipulating a 'books' table.

Uploaded by

sachinnayaka999
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)
7 views

Day_2_SQL_Notes

The document provides an overview of SQL data modification commands including INSERT, UPDATE, and DELETE, along with their syntax and examples. It also covers basic SQL functions such as string, numeric, and date functions, with exercises for practice. Additionally, it includes practice queries for creating and manipulating a 'books' table.

Uploaded by

sachinnayaka999
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
You are on page 1/ 6

Day 2: SQL - Data Modification & Simple Functions

### 1. Modifying Data in SQL

#### 1.1 Inserting Data (INSERT INTO)

The INSERT INTO command adds new records into a table.

Syntax:

INSERT INTO table_name (column1, column2, column3, ...)

VALUES (value1, value2, value3, ...);

Example:

INSERT INTO students (student_id, student_name, age, grade_level)

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

Exercise:

- Insert a student with ID 2, named "Bob", aged 21, in the "Junior" grade level.

#### 1.2 Updating Data (UPDATE)

The UPDATE command modifies existing records in a table.

Syntax:

UPDATE table_name

SET column1 = value1, column2 = value2, ...


WHERE condition;

Example:

UPDATE students

SET age = 21

WHERE student_id = 1;

Exercise:

- Update Bob's grade level to "Senior".

#### 1.3 Deleting Data (DELETE)

The DELETE command removes records from a table.

Syntax:

DELETE FROM table_name

WHERE condition;

Example:

DELETE FROM students

WHERE student_id = 1;

Exercise:

- Delete Bob's record from the table.

### 2. Basic SQL Functions


#### 2.1 String Functions

UPPER(): Converts text to uppercase.

Example:

SELECT UPPER('hello world'); -- Output: 'HELLO WORLD'

LOWER(): Converts text to lowercase.

Example:

SELECT LOWER('HELLO WORLD'); -- Output: 'hello world'

CONCAT(): Concatenates two or more strings.

Example:

SELECT CONCAT('Hello', ' ', 'World'); -- Output: 'Hello World'

Exercise:

- Convert the name "Alice" to uppercase.

- Concatenate "Alice" with "Sophomore" to get "Alice Sophomore".

#### 2.2 Numeric Functions

COUNT(): Returns the number of rows.

Example:

SELECT COUNT(*) FROM students;

SUM(): Adds up numeric values in a column.

Example:

SELECT SUM(age) FROM students;


AVG(): Calculates the average value of a column.

Example:

SELECT AVG(age) FROM students;

Exercise:

- Count the total number of students.

- Find the average age of students.

#### 2.3 Date Functions

NOW(): Returns the current date and time.

Example:

SELECT NOW();

CURDATE(): Returns the current date.

Example:

SELECT CURDATE();

Exercise:

- Retrieve the current date.

### 3. Practice Queries for the 'books' Table

1. Create a table 'books':

CREATE TABLE books (

book_id INT PRIMARY KEY,


title VARCHAR(255),

author VARCHAR(255),

price DECIMAL(10, 2)

);

2. Insert 3 books into the table:

INSERT INTO books (book_id, title, author, price)

VALUES

(1, 'The Great Gatsby', 'F. Scott Fitzgerald', 10.99),

(2, 'To Kill a Mockingbird', 'Harper Lee', 7.99),

(3, '1984', 'George Orwell', 8.99);

3. Update the price of one book:

UPDATE books

SET price = 9.99

WHERE book_id = 3;

4. Delete one book from the table:

DELETE FROM books

WHERE book_id = 2;

5. Count the total number of books:

SELECT COUNT(*) FROM books;

6. Find the sum of all book prices:

SELECT SUM(price) FROM books;


7. Retrieve the current date:

SELECT CURDATE();

You might also like