SQL Query Writing Part-I

Bhagya Mangale
4 min readApr 25, 2020

--

In this article we are going to understand how to write SQL queries So let’s see…

Their are so many queries present into SQL Server like Insert, Update, Delete, Select, Drop, Truncate, Alter and so on…

In this article we are going to see ‘SELECT’ query with different condition’s applying with it.

*So first we’ll know What is SELECT Query/Statement ?

  • SELECT is DML i.e (Data Manipulation Language) command. A SELECT statement is used to retrieve the rows from one or more database tables. In most applications a SELECT statement is the most commonly used.
  • The SELECT statement has many optional clause.

a). FROM

b). WHERE

c). GROUP BY

d). HAVING

e). ORDER BY

f). AS

*So we’ll see how to use SELECT statement with different condition’s…

1. Select all Records:

SELECT *
FROM table_name;

  • This query is used to see all the records which are present into table.

2. Select with a where clause:

SELECT *
FROM table_name
WHERE column_name=value;

  • In this query the WHERE clause is used to add some condition with SELECT query, See the example
  • In above example we can see that we fire a query ‘SELECT * FROM tblEmp WHERE EmpId=101’ in this query we used WHERE clause to select all data from the table which have that employee Id, in above example we can see the result.

3. Select with Numeric and String:

  • In SQL query when we use WHERE clause, in where clause if we use string then we must to add a value inside the ‘ ’ OR “ ” (Single quotes/Double quotes), otherwise it will give you an error. And if we use numeric then you can add value without any quotes.

#Query Example 1

SELECT *
FROM tblEmp
WHERE EmpName=’shree’

  • In above example we have search all records from table on WHERE clause String so we use ‘’(Single quotes) for it.
  • If you do not use the single or double quotes for string value then it will give below error.

#Query Example 2

SELECT *
FROM tblEmp
WHERE EmpId=102

  • In this example we have search all records on WHERE clause Numeric so we haven’t use any quotes for a value.

4.Select with wild card:

  • In SQL their are multiple Wildcard operators are used like %wildcard,_wildcard but in this article we’ll see % wildcard.
  • Wild Card operator contain patterns which are used to search names and numbers also.Following are the patterns of the Wild Card Operator.

i. ‘Shree%’ :- This wildcard pattern show any values that starts with ‘Shree’.
ii. ‘%Shree%’ :- This wildcard pattern show any values that have ‘Shree’ in any position.
iii. ‘%Shree’ :- This wildcard pattern show any values that end with ‘Shree’.

*Let’s see examples

#Example1

  • In above example we use pattern-i i.e ‘Shree%’ and it show’s that all values which are starts with ‘shree’.

#Example2

  • In this second example we use pattern-ii i.e ‘%Shree%’ and it show’s that all values which have the position of ‘shree’ anywhere.

#Example3

  • In this third example we have use pattern-iii i.e ‘%Shree’ and it show’s that all values which are end with ‘shree’.

5. Select specific columns with alias:

SELECT column_name
FROM table_name;

  • Select a specific column means if we don’t want all the column’s from table we just want a single or 2 columns from table then we can use this query.
  • And a specific column with alias means when we search a specific column it will show the column name which is used into table so for show that column to outsiders with different name we can use ‘alias’. see below

SELECT column_name AS new_column_name
FROM table_name;

  • In above image we can see the original column name is change with new column name.

So in this article we have see how to write SELECT query in SQL with some condition’s , We’ll see remaining SELECT queries with some other condition’s in next part till then watch following video.

Learn Step By Step Happily :-)

--

--