Retrieving and Manipulating Data With SQL


Retrieving and Manipulating Data With SQL . When interacting with databases, you will use four distinct types of operations Create, Read, Update, and Delete.

Reading Data
To read data from a database:-
First, you need to indicate the columns from the table that you want to retrieve for a query. You will do that with the SELECT statement. You need to indicate the table(s) you want to select the data from using the FROM keyword. Then you need a way to filter the data, making sure only the rows you are interested in are returned. You can filter the data using the WHERE clause in the SQL statement. Finally, you can order your results using the ORDER BY clause.

Selecting Data

To read data from one or more database tables, you use the SELECT statement. In its most basic form, the SELECT statement looks like this:

SELECT ColumnName [, OtherColumnNames] FROM TableName

Here, the parts between the square brackets are considered optional. For example, to retrieve all rows from the table and select only their Id and Name columns, you use this SQL statement:

SELECT EmpId, EmpName FROM CSITQuestion

Right after the SELECT statement comes a comma-separated list of column names. You can have only one or as many columns as you like here. Instead of specifying the column names explicitly, you can also use the asterisk (*) character to retrieve all the columns.

Filtering Data

To filter data, you use the WHERE clause, with which you indicate the criteria that you want your data to match. For example,

SELECT EmpID FROM CSITQuestion WHERE Name = ‘Aman’

Ordering Data

The ORDER BY clause comes at the end of the SQL statement and can contain one more column names or expressions, which can optionally include ASC or DESC to determine if items are sorted in ascending order (with ASC, which is the default if you leave out the keyword) or in descending order (using DESC).For Example:


Creating Data

To insert new rows in a SQL Server table, you use the INSERT statement.

INSERT INTO TableName (Column1 [, Column2]) VALUES (Value1 [, Value2])

Updating Data

To update data in a table, you use the UPDATE statement:

UPDATE TableName SET Column1 = NewValue1 [, Column2 = NewValue2] WHERE Column3 = Value3

Deleting Data

You can use the WHERE clause in a DELETE statement to limit the number of rows that get deleted.