LINQ vs SQL. Learn the difference

Facebook
Twitter
LinkedIn

Never miss a post!

Sign up for our newsletter and get FREE Development Trends delivered directly to your inbox.

You can unsubscribe any time. Terms & Conditions.
Categories

If you are confused between LINQ and SQL before starting your web or desktop application, we are here to shed some light on it, which will going to help you in understanding the concepts of both terms. To make a long story short, LINQ is a framework used to develop applications mainly rapid applications, which can support the MS SQL database and can map SQL tables to the classes with the .NET framework. On the other hand, SQL is a standard database query language to manage the structured data in relational databases.

What is LINQ?

LINQ stands for Language Integrated Query (pronounced as link) is a component in .NET framework, which enables you to write native data queries in .NET languages. It is designed by Microsoft Corporation to extend the capabilities of .NET languages in terms of data fetching and processing relational databases and third-party data sources.

What is SQL?

SQL stands for Structured Query Language (pronounced as sequel) is a language to manage data stored in relational database management systems particularly handling the structured data. It allows accessing the multiple records with a single statement and secondly any record can be accessed without giving an index to it. The programmer can write SQL queries to handle databases from its creation to the monitoring.

History of LINQ

Microsoft Corporation introduced LINQ in .NET framework 3.5 in 2007. It really changed on how we can write code to fetch data from the database and similarly store the data.

History of SQL

In the early 1970s, Donald D. Chamberlin and Raymond F. Boyce developed SQL at IBM. Initially, it was designed to store data in IBM database management system as SQUARE (Specifying QUeries in A Relational Environment), but later it was renamed to SEQUEL. The name was changed to SQL after dropping the vowels from it as it was already a trademark of a UK company.

Syntax in LINQ

There are two ways to write a query in LINQ

  • Query Syntax

Query Syntax is also called as Query Expression Syntax. It is similar to the SQL used for the databases i.e. it starts with ‘from’ keyword and ends with ‘select’ keyword.

The following example shows on how to fetch a list of strings having a word ‘Language’ using Query Syntax:

// List of random words 
IList stringKeywordList = new List() { 
    "C# Language",
    "SQL Functions",
    "Java Tutorials",
    "Python Language" ,
    "MVC framework" ,
    "C++ Tutorials" 
} ;

// Using LINQ Query Syntax to match the criteria
var resultList  = from item in stringKeywordList
                            where item.Contains("Language") 
                            select item;

//The item is the range variable of sequence IEnumerable and Contains is a conditional expression. //Whereas, where and select are the standard query operators. 
  • Method Syntax

Query Syntax is also called as Query Expression Syntax. It uses extension methods that are included in the Queryable and Enumerable classes. The complier will convert the method syntax into query syntax at compilation time.

The following example shows show on how to fetch a list of employees who are near to their retirement period based on their age:


// List of employees 
IList employeeList = new List() { 
        new Employee() { EmployeeID = 1, EmployeeName = "Alie", Age = 45} ,
        new Employee() { EmployeeID = 2, EmployeeName = "Tom",  Age = 62 } ,
        new Employee() { EmployeeID = 3, EmployeeName = "Donald",  Age = 57 },
        new Employee() { EmployeeID = 4, EmployeeName = "Johns" , Age = 35} ,
        new Employee() { EmployeeID = 5, EmployeeName = "Emma" , Age = 30 } 
    };

// using LINQ Method Syntax to match the criteria
var employees = employeeList.Where(e => e.Age >= 55 && e.Age <= 60)
                                  .ToList(); 
The extension method Where() is a part of Enumerable class. 

Syntax in SQL

SQL database contains one or more tables, which are identified by a unique name. The tables will have records known as rows with data. The SQL keywords are not case sensitive i.e. select and SELECT both have the same impact in the query.

There are some following important commands in SQL:

  • SELECT – It fetches data from a database
  • UPDATE – It updates data in a database
  • DELETE – It deletes data from a database
  • INSERT INTO – It inserts new data into a database

The following template is used to fetch the data from a table in a SQL database:

SELECT column1, column2, ...
FROM tableName
WHERE condition;
// Using select statement to fetch active employees 
Select * from tblEmployees where IsActive = 0 
Facebook
Twitter
LinkedIn

Our website uses cookies that help it to function, allow us to analyze how you interact with it, and help us to improve its performance. By using our website you agree by our Terms and Conditions and Privacy Policy.