Quantcast
Channel: CodeSection,代码区,SQL Server(mssql)数据库 技术分享 - CodeSec
Viewing all articles
Browse latest Browse all 3160

Diving Into The Basics of SQL Server

$
0
0

Let’s start our journey practically with SQL. I’ve attached the Chinook files; you can just run the script and make your environment ready for work. To learn the database, most of the time, we need to build the schema and put some data inside. But to speed up the learning and make our hands dirty with code as soon as possible, large organizations provide us the pre-built database and to make some experiments on it. So here, we’re using Chinook database and I’m working on SQL Server 2014.

The examples are so important to know the implementation of the things. It makes it extremely easy for a developer or DBA to understand what is happening under the hood. So, in our complete series, we’ll mostly work with examples and discuss a little bit about the results of those example queries.

Order By

The Order By clause is used to arrange the results in Ascending or Descending order.

Example 1 SELECTInvoiceId ,InvoiceDate ,Total FROMInvoice WHERETotal>10 ORDERBYTotal

Here, we’re displaying the invoice information and applying the check with the help of Where clause if Total is greater than 10. And then, we’re ordering the results by Total in an ascending order.

As we’ve not explicitly mentioned any order type, it is by default in ascending order.

Example 2

Now, we want to display the records in descending order. So,

SELECTInvoiceId ,InvoiceDate ,Total FROMInvoice WHERETotal>10 ORDERBYTotalDESC BETWEEN & AND

Now, we want to display the items within aspecific range. So for that, we’ll use BETWEEN & AND. These are the reserved keywords in SQL actually. Let’s see how we use them in SQL.

SELECTInvoiceId ,InvoiceDate ,Total FROMInvoice WHERETotal BETWEEN10.39 AND15.9 ORDERBYTotal

Now, you might wonder if it is necessary to use both the keywords at the same time. Well, the answer will be "No, we can write the same query without BETWEEN as well".

SELECTInvoiceId ,InvoiceDate ,Total FROMInvoice WHERETotal>=10.39 ANDTotal<=15.9 ORDERBYTotal

And now, you’ll get the same number of rows in the result.

Example 2

Let’s filter the records on the basis of date.

SELECTInvoiceId ,InvoiceDate ,Total FROMInvoice WHEREInvoiceDate BETWEEN'2012-01-01' AND'2012-12-31' ORDERBYInvoiceDate

Actually, if you didn’t specify the time, SQL Server stores the time of midnight (00:00:00:000) with the data given by the user automatically. And if you run this script, as you’ve not any demand of time range, obviously you’ll see all the records between these 2 dates. And if you’ve input the time in the table for data, then you’ll see the results where the time will be (00:00:00:000).

Alias or Assign

Alias is the thing which we use to rename our attribute with the help of as keyword.

SELECTInvoiceId ,UnitPrice ,Quantity ,UnitPrice*QuantityASTotal FROMInvoiceLine

If you see the results of the above code in the Output window, you’ll see 4 attributes containing data - InvoiceId, UnitPrice, Quantity, and Total.

Now, let’s suppose we don’t want to use the "as"keyword. So, we’ll use the increment operator.

SELECTInvoiceId ,UnitPrice ,Quantity ,Total=UnitPrice*Quantity FROMInvoiceLine

Now again, you’ll see the same result.

LEFT FUNCTION

With the help of Left() function, we get the letters from the left side of how much we specified.

SELECTEmployeeId ,LEFT(FirstName,1)+''+LastNameASFullName ,City FROMEmployee ORDERBYFirstName

Let us run the query and watch the results in the Output window.


Diving Into The Basics of SQL Server

Look, we get the first letter of FirstName with the help of LEFT() function.

How to find Age from Date of Birth?

Here, we’ll use DateDiff() sql function to find out the difference from Date of Birth to today’s date and we’ll get the current date dynamically. Let’s see what happens,

SELECTEmployeeId ,FirstName ,LastName ,BirthDate ,Convert(varchar(10),BirthDate,10)ASFormattedBirthDay ,DateDiff(Year,BirthDate,GetDate())ASAge FROMEmployee

SQL Server is basically using Visual Studio Shell under the hood. So you might see that when your Visual Studio Environment is not ready, your SQL Server Management Studio also doesn’t work. The purpose of this talk is to tell you that in SSMS, we also have the feature of IntelliSense. If we write the function, then with the help of IntelliSense, we know what arguments we can input inside the function.

Here is the result of our query.


Diving Into The Basics of SQL Server

Now you might be thinking of how Convert() works, here is the detailed link . But still, you might be confused about what is 10 inside Convert(). Actually, it is the DateTime style. On MSDN, all the Date and Time styles are mentioned .

How to Show Different Records?

Here, we are accessing all the Billing Cities from each record in the table.

SELECTBillingCity FROMInvoice ORDERBYBillingCity

But now, we want to show the distinct Billing Cities. However, a single city name is available in multiple records but we want distinct. So, let's write this query.

SELECTDISTINCTBillingCity FROMInvoice ORDERBYBillingCity

And now, you’ll see all the distinct records.

Top 5 Records

SQL Server has a nice feature to select top records.

SELECTTOP5InvoiceId,TotalFROMInvoice ORDERBYTotal

If we want to show the 5% records of the table, we write like this.

SELECTTOP5PERCENTInvoiceId,TotalFROMInvoiceORDERBYTotal Total Number of Records in a table SELECTCOUNT(*)FROMInvoice

So, it will show the all number of records in a table as an Output.

Not Equal To

There are two syntaxes of not equal to in SQL Server (<> & !=)

SELECTInvoiceId ,InvoiceDate ,BillingState ,BillingCity ,Total FROMInvoice WHEREBillingState<>NULL ORDERBYBillingState

And similarly, we can write the same statement in this way.

SELECTInvoiceId ,InvoiceDate ,BillingState ,BillingCity ,Total FROMInvoice WHEREBillingState!=NULL ORDERBYBillingState

Yes, we have another variation to write the query as well.

SELECTInvoiceId ,InvoiceDate ,BillingState ,BillingCity ,Total FROMInvoice WHEREBillingStateISNOTNULL ANDBillingState>'B' ORDERBYBillingState

In this query, it will check if the BillingState contains the letter greater than B.

Little Confusion SELECTInvoiceId ,I

Viewing all articles
Browse latest Browse all 3160

Trending Articles