Let’s dive into the Top 10 SQL window function queries that you must know.
Saturday, July 19, 2025
Top 10 SQL Queries from Technical Interviews to Learn Window Functions like ROW_NUMBER
Hello guys, If you're preparing for SQL interviews or working on real-world reporting tasks, mastering window functions is no longer optional. They are one of the most powerful tools in SQL which allows you to perform calculations across rows related to the current row without collapsing your result set like GROUP BY does. Window functions like ROW_NUMBER(), RANK(), LEAD(), LAG(), and SUM() OVER help you write cleaner, faster, and more expressive queries—ideal for performance tuning and analytical processing.
Labels:
Microsoft SQL Server
,
SQL
,
SQL Interview Questions
Friday, April 4, 2025
How to add Primary key into a New or Existing Table in SQL Server? Example
Since a primary key is nothing but a constraint, you can use the ALTER clause of SQL to add a primary key into the existing table. Though it's an SQL and database best practice to always have a primary key in a table, you will often find tables that don't have a primary key. Sometimes, this is due to lack of a column that is both NOT NULL and UNIQUE (constraint require to be a primary key), but other times purely due to lack of knowledge or lack of energy. If you don't have a column that can serve as the primary key, you can use identity columns for that purpose. Alternatively, you can also combine multiple columns to create composite primary keys, like you can combine firstname and lastname to create a primary key name, etc.
Labels:
database
,
Microsoft SQL Server
,
SQL
Wednesday, April 2, 2025
How to increase length of existing VARCHAR column in SQL Server? Example Tutorial
You can increase the length of a VARCHAR column without losing existing data in SQL Server. All you need to do is execute the following ALTER TABLE statements. Though, you need to specify NULL or NOT NULL constraint explicitly, depending upon your data.
Here is the SQL command you can use to increase the length of a VARCHAR column in SQL Server:
ALTER TABLE Books ALTER COLUMN title VARCHAR (432)
This command increases the length of the title column of the Books table to 432 characters. You can use the same command to increase the length of CHAR, NCHAR, or NVARCHAR columns as well.
Here is the SQL command you can use to increase the length of a VARCHAR column in SQL Server:
ALTER TABLE Books ALTER COLUMN title VARCHAR (432)
This command increases the length of the title column of the Books table to 432 characters. You can use the same command to increase the length of CHAR, NCHAR, or NVARCHAR columns as well.
Labels:
database
,
Microsoft SQL Server
,
SQL
Subscribe to:
Posts
(
Atom
)