SQL Query Writing (PART V)

Nilam Shikhare
4 min readApr 28, 2020

--

In this article we are going to learn about some SQL queries.

  • Structured Query Language(SQL) is database language by using this we can perform certain operations on the existing database.
  • A query is used to request for data in to a database.
  • In SQL queries can be categorized as-
  1. DDL — Data Definition Language
  2. DML — Data Manipulation Language

SELECT Statement:

  • Select Statement can be used to retrieve record from the database.
  • If we can to show all records from the table then * sign can be used.

SELECT ALL RECORDS:

Consider Following table tblTeacher

Syntax:

SELECT * FROM TABLE_NAME

Example:

SELECT * FROM tblTeacher

SELECT WITH WHERE CLAUSE:

  • From this above table if we want to select only specific table then where clause can be used.

Syntax:

SELECT * FROM TABLE_NAME

WHERE condition

Example:

  • If we want to retrieve only the TeacherID,FirstName then query like-

SELECT TeacherID,FirstName

FROM tblTeacher

WHERE TeacherID=1002

SELECT WITH NUMERIC AND STRING:

NUMERIC:

  • If we want to retrieve the information from the table by using any numeric value then equal to sign(=) can be used.

Example:

STRING:

  • If we want to retrieve the information from the table by using any string value then equal to sign(=) then single quote(‘’) can be used.

Example:

SELECT WITH WILDCARD:

  • If we want to search any record with a specific pattern then LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

There are two wildcards often used with the LIKE operator:

  • %=The percent sign represents zero, one, or multiple characters
  • _=The underscore represents a single character

Syntax:

SELECT COLUMN_NAME(S)
FROM TABLE_NAME
WHERE COLUMN_NAME LIKE ‘%ABC%’

Example 1:

Example 2:If we can to display only those records whose Subject name ends with ‘lar’ then query like-

Example 3:Finds any values that have S in 1st position and L in 3rd position.

Example 4:Finds any values that have A in 1st position and are at least 4 characters in length.

SPECIFIC COLUMN WITH ALIAS:

  • It can be used to give a temporary name to the column.
  • And it only exists for the duration of the query

Syntax:

SELECT COLUMN_NAME AS NEW_COL_NAME

FROM TABLE_NAME

Example:

SELECT Salary AS Payment

FROM tblTeacher

SELECT WITH INNER JOIN:

  • When we do inner join,It can returns only those records which have

matching values in both the table.

Syantax:

SELECT COLUM_NAME(S)

FROM TABLE1

INNER JOIN TABLE2 ON

TABLE1.COLUMN_NAME=TABLE2.COLUMN_NAME;

Example:

SELECT WITH LEFT JOIN:

When we do left join,it can show all records from the left side of table and only matching records in right side of table.

Syntax:

SELECT COLUM_NAME(S)

FROM TABLE1

LEFT JOIN TABLE2 ON

TABLE1.COLUMN_NAME=TABLE2.COLUMN_NAME;

Example:

SELECT WITH RIGHTJOIN:

When we do right join,it can show all records from the right side of table and only matching records in left side of table.

Syntax:

SELECT COLUM_NAME(S)

FROM TABLE1

RIGHT JOIN TABLE2 ON

TABLE1.COLUMN_NAME=TABLE2.COLUMN_NAME;

Example:

To understand more about SQL Queries must watch following video.It’s really help you…….

Thank You…..!!!

--

--