<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kurmapu Hymavathi</title>
    <description>The latest articles on Forem by Kurmapu Hymavathi (@kurmapu_hymavathi_9e47877).</description>
    <link>https://forem.com/kurmapu_hymavathi_9e47877</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3577012%2Ffffaac96-efdf-489a-959f-cb6ddd92c5f2.png</url>
      <title>Forem: Kurmapu Hymavathi</title>
      <link>https://forem.com/kurmapu_hymavathi_9e47877</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kurmapu_hymavathi_9e47877"/>
    <language>en</language>
    <item>
      <title>SQL Query Optimization for Beginners</title>
      <dc:creator>Kurmapu Hymavathi</dc:creator>
      <pubDate>Tue, 21 Oct 2025 10:17:27 +0000</pubDate>
      <link>https://forem.com/kurmapu_hymavathi_9e47877/sql-query-optimization-for-beginners-548a</link>
      <guid>https://forem.com/kurmapu_hymavathi_9e47877/sql-query-optimization-for-beginners-548a</guid>
      <description>&lt;p&gt;Query Optimization is a process of writing a SQL query to improve the database performance. When I started learning SQL, I have noticed that even small improvements reduced the query execution time. Below are the few practical tips I use whenever I write SQL queries:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Use specific columns instead of * to get all records&lt;br&gt;
&lt;code&gt;select EmployeeId,EmpName,city from Employee&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use Group by clause instead of distinct if possible&lt;br&gt;
&lt;code&gt;select distinct Department from Employee&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Below query also gives the same result but this has some query        improvements.&lt;br&gt;
&lt;code&gt;select Department from Employee Group by Department&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;3.Use primary key while creating a table&lt;br&gt;
&lt;code&gt;&lt;br&gt;
Create table Employee (&lt;br&gt;
id int constraint id_employee primary key,&lt;br&gt;
name varchar (50)&lt;br&gt;
);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;4.Use top/limit to preview query results&lt;br&gt;
&lt;code&gt;select top 3 * from Employee order by 1 desc&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;5.Indexing the right columns - Adding index is always better for the select operation.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;select * from Employee&lt;br&gt;
create index idx_department_employee on Employee (Department)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;6.When you want to create a new stored procedure, you should always create a one stored procedure started with usp_ not with sp_. if you try to create stored procedure started with sp_ it will first try to look into the master database and then it will go to your database but at the same time when you have created the store procedure started with usp_, it will go directly to your database and it will search for that stored procedure. so, using this usp_(User Stored Procedure) is a bit faster than sp_.&lt;/p&gt;

&lt;p&gt;Bad Approach:&lt;br&gt;
&lt;code&gt;CREATE PROCEDURE sp_GetEmployee&lt;br&gt;
AS&lt;br&gt;
BEGIN&lt;br&gt;
SELECT * FROM Employee;&lt;br&gt;
END&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Good Approach:&lt;br&gt;
&lt;code&gt;CREATE PROCEDURE usp_GetEmployee&lt;br&gt;
AS&lt;br&gt;
BEGIN&lt;br&gt;
SELECT * FROM Employee;&lt;br&gt;
END&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;7.We should always use nolock when we are querying something&lt;/p&gt;

&lt;p&gt;we are querying this employee table and assume this table has a lot of records and the query is bit complex then in that case if your transaction got a stuck then other people who wants to query this table on the same server will not be able to do so because this table has been locked for that same reason we should always use &lt;strong&gt;nolock&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bad approach:&lt;br&gt;
&lt;code&gt;SELECT EmpName FROM EMPLOYEE WHERE EmpId=7&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Good approach:&lt;br&gt;
&lt;code&gt;SELECT EmpName FROM EMPLOYEE WITH (NOLOCK) WHERE EmpId=7&lt;br&gt;
                     OR&lt;br&gt;
SELECT EmpName FROM EMPLOYEE (NOLOCK) WHERE EmpId=7&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;8.If possible, use wildcards only at the end of the phrase.&lt;br&gt;
&lt;code&gt;SELECT EmpName FROM EMPLOYEE WHERE DEPARTMENT='H%'&lt;/code&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>beginners</category>
      <category>database</category>
    </item>
  </channel>
</rss>
