Tuesday, August 6, 2024

SQL - SELECT | Select Distinct | Insert Into | Insert Multiple rows | Update | delete

SELECT statement:

It is used to select or view data from a database table.

Syntax:

SELECT column1, column2, ...
FROM table_name;

Or to get all columns

SELECT *
FROM table_name;

Example:

SELECT CustomerName, City FROM Customers;


Select distinct statement:

It is used to return or select distinct or different values.

Syntax:

SELECT DISTINCT column1, column2, ...
FROM table_name;

Example:

SELECT DISTINCT Country FROM Customers;

Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.


Insert into statement:

It is used to insert new records to a db table.

Syntax:

Two ways we can do that

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal''Tom B. Erichsen''Skagen 21''Stavanger''4006''Norway');

2. If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query. However, make sure the order of the values is in the same order as the columns in the table

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

Insert Multiple Rows

It is also possible to insert multiple rows in one statement.

To insert multiple rows of data, we use the same INSERT INTO statement, but with multiple values:

INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES
('Cardinal''Tom B. Erichsen''Skagen 21''Stavanger''4006''Norway'),
('Greasy Burger''Per Olsen''Gateveien 15''Sandnes''4306''Norway'),
('Tasty Tee''Finn Egan''Streetroad 19B''Liverpool''L1 0AA''UK');


Update:

The UPDATE statement is used to modify the existing records in a table.

Syntax:

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

Delete:

The DELETE statement is used to delete existing records in a table.

DELETE FROM table_name WHERE condition

DELETE FROM Customers WHERE CustomerName='Sanddy';

Delete all records:

DELETE FROM table_name;

Delete a table:

DROP TABLE table_name;


No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...