Select sentences

In this article you are able to find out basic syntax for how to use select sentences. This has been tested on Microsoft SQL Server so this might differ if you are using Oracles or MySQL database management systems (DBMS).

Basic syntax

The most basic way to check is there anything in your database is first to look there from graphical user interface. Sometimes this isn’t an option and it might take some time to application to show data rows. In this case, you might consider using SQL-language as your ultimate searching tool. Here is an most basic syntax

SELECT * FROM table_name;

!!!NOTE!!!

This isn’t the way you should test databases, but in learning experince you might want to quick chekck database tables. So why this should not be used in company standards. When you use * in SQL, it means that it selects all attributes from table. Imagine what would happen when you use it in database where there can be 20-50 attributes and overall data rows 100 000 or even more. It tooks some time even database management system to proceed that kind of query. It could sometimes even crash the database or freeze it.

Better way to use SELECT commands is to add some boundaries to selection. Most used is to select only certain attributes. So the better way to do query is:

SELECT attribute_1 FROM table_name

To make more effective query is to add even more boundary conditions and one way is to add “Where” –statement. That means the most approved way to make query is:

SELECT attribute_1 FROM table_name WHERE attribute_2 < 10 000;

As you can see, you are able to comparison sentences in your SELECT queries. The other possible comparison statements are:

  • <
  • >
  • <=
  • >=
  • =
  • !=

You are also able to use logical statements like: AND, OR, IN, BETWEEN…AND…, IS NULL, IS NOT NULL, NOT IN.

Advanced queries

With above skills you are able to make most of the SELECT -statements. But there are even more ways to upgrade your queries so here is a little list of them

  • DISTINCT
  • ORDER BY (ASC, DESC)
  • GROUP BY
  • HAVING
  • JOIN…ON…
  • dsf
  • fdsf

And then you are able to use some already build on features in SQL. Some of them are:

  • COUNT (attribute) AS ‘new name’
  • SUM (attribute) AS ‘new name’
  • MIN (attribute) AS ‘minimum’
  • MAX (attribute) AS ‘maximum
  • AVG (attribute) AS ‘avarage’

Conclusion

If you would like to get some data from database using SQL-queries SELECT –sentences is the one way to go. The other possible way is to use graphical user interfaces but if you have direct access to your database via database management, using direct queries is fastest way to do it.

– Tuomas Törmä

Sources

  • Introduction to Database Systems lectures At Haaga-Helia autumn 2013 teacher Tanja Bergius.

Leave a comment