• Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Have any question?

(+91) 98222 16647
info@simplycoding.in
RegisterLogin
Simply Coding
  • Home
  • Courses
  • School
  • Programs
  • Problems
  • Contact Us
  • My account
  • Register

Data Management

SQL Select Statement: Tutorial

  • Categories Data Management, Database

In this post we are going to learn how to make simple queries in SQL.

For the purpose of this exercise, we will consider this small table in a database:

We will first learn how to insert a new row/record/tuple in a relation. If you see the syntax, we use INSERT INTO tablename command.

What is displayed in square brackets is optional. Column names can be skipped if we are inserting values in all columns of the table in order.

If we are entering data in only few columns, then number of column names and values need to match.

As you see, using this command we can add multiple rows to the table.

Once you have inserted the data, you can fetch data from a relation. The syntax for select is as shown.

We can either use star to get all columns or we can specify individual attribute names to get specific columns.

For e.g. if in our table we mention * it will return all columns but if we specify only 2 attributes, then only those 2 columns are returned. 

DISTINCT can be used if we want only distinct values to be returned. For e.g. if we want individual cities, we use DISTINCT command.

                                                                   

You can also use Mathematical operations between 2 columns. For e.g. if you want to know total amount of the order, you can use this syntax and use multiplication operator between them.

Any of the operators shown can be used where % refers to remainder or modulus operator.

Now in all of these select queries, all the rows or tuples are being displayed. If we want only select rows, we expand this query to include a WHERE clause.

WHERE clause is used to specify a condition for SELECT.  Now the condition could be any of the following. Let’s look at each one of them.

First is using relational operators.  We can use equal to, less than, greater than or combination of these in our query. For e.g. if we want all rows which have city equal to Delhi we will use where city = ‘delhi’. 

Or if we want all rows where quantity ordered is greater than 10 then we will use where quantity > 10.

Do note the quotes for all non-numeric values.

Next is condition based upon range. Here we use the keyword BETWEEN if we want to select orders within a range.

We also use NOT before between if we want the opposite result.

Next is condition based upon a list. If you want to select values which are part of given list. Let’s say all customers in particular cities.

Here too you can use NOT if you want opposite result.

SQL also has a text matching operator LIKE where you can use wildcard characters like %  or underscore. This can be used for pattern matching, like all address which have Nagar or all names of five letters etc.

                                                                            

You can also use logical OR, AND to combine two conditions in where clause.

For e.g. showing all quantity > 150 and price > 500.

You can also use OR where you can give quantity > 150 or price > 500.

In a select clause like where you can optionally give ORDER BY clause. Here you specify the column on which you want the sorting to happen and whether it needs to be in Ascending or descending order.

Once you have inserted tuples in a relation you update or delete them using similar where clause.

To update we use the Update tablename SET command where we give the columns and the revised values. The where clause tells which rows are to be updated. If multiple rows are selected, then for all of them attribute is modified.

To delete we use delete FROM tablename command. If no where is specified it will delete all rows.

Check out our video: https://youtu.be/UqIaAgAW9uUta

  • Share:
author avatar
Simply Coding

Previous post

SQL: Join
December 14, 2020

Next post

Index Commands in SQL
December 14, 2020

You may also like

Screenshot_20201209-112442__01
Index Commands in SQL
14 December, 2020
Screenshot_20201209-111403__01
SQL: Join
10 December, 2020
Screenshot_20201209-124632__01
SQL: Introduction
10 December, 2020

Leave A Reply Cancel reply

You must be logged in to post a comment.

Categories

  • Uncategorized
  • Programs
    • Python
    • Java
  • Problems
    • Python
    • Java
    • Web Development
      • Internet
    • Emerging Technologies
  • Notes
    • General
    • QBasic
    • MS Access
    • Web Development
      • XML
      • HTML
      • JavaScript
      • Internet
    • Database
    • Logo Programming
    • Scratch
    • Emerging Trends
      • Artificial Intelligence
      • Internet of Things
      • Cloud Computing
      • Machine Learning
    • Computer Fundamentals
      • Computer Networks
      • E-Services
      • Computer Hardware
    • Python
    • Java
  • School
    • ICSE
      • Computers Class 9
        • Java Introduction
        • Tokens & Data Types
        • Java Operators
        • Math Library
        • if & switch
        • For & While
        • Nested loops
      • Computer Class 10
        • Sample Papers
        • OOPS concepts
        • Functions in Java
        • Constructors
        • Arrays in Java
        • Strings in Java
    • SSC
      • IT Class 11
        • IT Basics
        • DBMS
        • Web Designing
        • Cyber Laws
      • IT Class 12
        • Web Designing
        • SEO
        • Advanced JavaScript
        • Emerging Tech
        • Server Side Scripting
        • E-Com & E-Gov
      • Computer Science 11
      • Computer Science 12
    • CBSE
      • Computer 9
        • Basics of IT
        • Cyber Safety
        • Scratch
        • Python
      • Computer 10
        • Sample Papers
        • Networking
        • HTML
        • Cyber Ethics
        • Scratch
        • Python
      • Computer Science 11
        • Computer Systems
        • Python 11
          • Python Basics
          • Python Tokens
          • Python Operators
          • Python if-else
          • Python loops
          • Python Strings
          • Python List
          • Python Tuple
          • Python Dictionary
          • Python Modules
        • Data Management
      • Computer Science 12
        • Sample Papers
        • Python 12
          • Python Functions
          • Python File Handling
          • Python Libraries
          • Python Recursion
          • Data Structures
        • Computer Networks
        • Data Management
    • ISC
      • Computer Science 11
        • Introduction to Java
        • Values & Data Types
        • Operators
        • if & switch
        • Iterative Statements
        • Functions
        • Arrays
        • String
        • Data Structures
        • Cyber Ethics
      • Computer Science 12
        • Sample Papers
        • Boolean Algebra
        • OOPS
        • Wrapper Classes
        • Functions
        • Arrays
        • String
Simply Coding Computer Courses for School                Privacy Policy     Terms of Use     Contact Us

© 2021 Simply Coding

Login with your site account

Lost your password?

Not a member yet? Register now

Register a new account

Are you a member? Login now