Monday 20 February 2023

SQL Server practical question with a possible solution: Top 3 Highest salary

 

Suppose you have a table named "employees" with the following columns: employee_id (int), first_name (varchar), last_name (varchar), hire_date (datetime), and salary (decimal). Write an SQL query to retrieve the top 3 highest-paid employees in the table.

Solution 1: You can use the "TOP" and "ORDER BY" clauses to retrieve the highest-paid employees. Here's an example query:

 

SELECT TOP 3 first_name, last_name, salary

FROM employees

ORDER BY salary DESC;

 

This query selects the first_name, last_name, and salary columns from the "employees" table, and then sorts the results by the "salary" column in descending order. The "TOP 3" clause limits the results to the top 3 rows.

Note: If there are ties for the fifth highest salary, the query will return all employees with that salary. If you want to limit the results to exactly 3 rows, you can use the "SELECT DISTINCT TOP 3" syntax instead.



there are other ways to write the query to retrieve the top 3 highest-paid employees from the "employees" table in SQL Server. Here are a few alternative solutions:


Solution 2: Using Limit Clause:

 

SELECT first_name, last_name, salary

FROM employees

ORDER BY salary DESC

LIMIT 3;


Note : "LIMIT" clause is not supported in SQL Server, but it is supported in some other database management systems such as MySQL and PostgreSQL.


Solution 3: Using Sub-query:


SELECT first_name, last_name, salary FROM employees WHERE salary IN ( SELECT TOP 3 salary FROM employees ORDER BY salary DESC );


This query uses a subquery to retrieve the top 3 highest salaries, and then selects the first_name, last_name, and salary columns for the employees with those salaries. The "WHERE salary IN" clause filters the results to only include employees whose salary is in the top 3.

Solution No -4 : Using the "RANK" function:

SELECT first_name, last_name, salary FROM ( SELECT first_name, last_name, salary, RANK() OVER (ORDER BY salary DESC) AS salary_rank FROM employees ) AS ranked_employees WHERE salary_rank <= 3;



This query uses the "RANK" function to assign a rank to each employee based on their salary, with the highest salary being assigned a rank of 1. The outer query selects the first_name, last_name, and salary columns for employees whose salary rank is 1-3.


What is the Log Shipping in SQL Server?

Log shipping is a disaster recovery solution for SQL Server that involves copying and restoring transaction log backups from a primary database server to one or more secondary database servers. The purpose of log shipping is to maintain a warm standby database on a secondary server that can be quickly activated in the event of a primary server failure or disaster.

Here's how log shipping works:

  1. On the primary server, transaction logs are backed up and copied to a shared location that is accessible by the secondary server.

  2. On the secondary server, the transaction logs are restored to a standby database, which is kept in a recovery mode that allows additional transaction logs to be applied as they become available.

  3. The secondary database can be configured to stay in a read-only state or in standby mode, which allows users to query the database, but not modify it.

  4. In the event of a primary server failure or disaster, the secondary database can be activated, and users can begin using it for read and write operations.


Log shipping provides a relatively low-cost disaster recovery solution for SQL Server, as it requires only standard backup and restore operations, and can be configured with built-in SQL Server functionality. However, log shipping does have some limitations, such as the potential for data loss if the transaction log backups are not frequent enough, and the need for manual failover and failback operations. As such, log shipping is typically used in conjunction with other disaster recovery solutions, such as clustering or Always On Availability Groups.

 

SQL Server Basic problems

 Here are a few common SQL Server problems and their solutions:

  1. Slow performance: One of the most common SQL Server problems is slow performance. This can be caused by a number of factors, including insufficient memory or disk space, inefficient queries, or a poorly designed database schema. To address this problem, you can try optimizing your queries, indexing your database, or upgrading your hardware.

  2. Database corruption: If your database becomes corrupt, you may be unable to access your data. To address this problem, you can try restoring from a backup, repairing the database using the DBCC CHECKDB command, or using a third-party recovery tool.

  3. Connection issues: If you're having trouble connecting to your SQL Server instance, it could be due to a firewall or network issue, an incorrect login or password, or a misconfigured server. To address this problem, you can check your firewall settings, double-check your login credentials, or try restarting the SQL Server service.

  4. Deadlocks: Deadlocks occur when two or more processes are blocked, waiting for each other to release a resource. To address this problem, you can try optimizing your queries to reduce contention, or implementing locking hints to control concurrency.

  5. Insufficient disk space: If you run out of disk space on your SQL Server instance, you may be unable to insert or update data. To address this problem, you can try deleting unnecessary data, archiving old data to a separate server, or adding more disk space to your server.

Saturday 18 February 2023

Asynchronous programming in javascript with example

 Asynchronous programming in JavaScript is a way to execute non-blocking code, allowing other parts of the program to continue running while waiting for a long-running operation to complete. Here's an example of asynchronous programming in JavaScript using callbacks:


function getData(callback) { // Simulate a long-running operation with setTimeout setTimeout(function() { const data = [1, 2, 3, 4, 5]; callback(data); }, 1000); } function displayData(data) { console.log(data); } // Call getData with displayData as a callback function getData(displayData); console.log("This code is executed before the data is retrieved.");



In this example, the getData function simulates a long-running operation using setTimeout, and takes a callback function as an argument. Once the operation is complete, getData calls the callback function with the resulting data.


The displayData function is defined as a separate function to handle the data once it is retrieved. This function is passed as a callback to getData.

Finally, getData is called with displayData as the callback function, and the program continues running while the data is retrieved. The last console.log statement is executed before the data is retrieved, demonstrating the non-blocking nature of asynchronous programming in JavaScript.

Example that demonstrates how "switchMap" works:

"SwitchMap " working :


Here are the perfect example to understand working of "switchmap"

Suppose we have an input field that users can type into to search for books in a library. As the user types, we want to make an HTTP request to a server to get book suggestions based on the input. However, we don't want to make a new request for every keystroke, as that would be inefficient. Instead, we want to wait for the user to pause typing for a certain amount of time before making the request.

import statement for angular

import { fromEvent } from 'rxjs'; import { switchMap, debounceTime, distinctUntilChanged } from 'rxjs/operators'; import { ajax } from 'rxjs/ajax';
/* Get data from the search box */ const searchBox = document.getElementById('search-box');
const bookSuggestions$ = fromEvent(searchBox, 'input').pipe( debounceTime(500), // wait 500ms after each keystroke distinctUntilChanged(), // only emit if the value has changed switchMap((event) => { const searchQuery = (event.target as HTMLInputElement).value; return ajax.getJSON(`https://mylibrary.com/books?q=${searchQuery}`); }) ); bookSuggestions$.subscribe((books) => console.log(books));


In this example we first import all the necessary module in angular to achieve functionality of
switchmap. we first create an Observable from the input event of the search box using the fromEvent function. We then use debounceTime(500) to wait for 500ms after each keystroke before emitting the event. This ensures that we don't make a new request for every keystroke. We also use distinctUntilChanged() to only emit events if the search query has changed.

Finally, we use switchMap to transform the emitted events into an Observable that makes an HTTP request to the server using the ajax function. If a new event is emitted while the previous request is still in progress, switchMap will unsubscribe from the previous request and make a new one based on the new event. This ensures that we always get the most up-to-date search results.

Friday 17 February 2023

An Introduction to JavaScript: The Language That Powers the Web

 Introduction:

JavaScript is one of the most popular programming languages in the world, and for good reason. It is the primary language used to create interactive web pages and dynamic user interfaces. In this blog post, we will give you a brief introduction to JavaScript and explain why it is such an important language for web development.

Section 1: What is JavaScript? JavaScript is a high-level, object-oriented programming language that is primarily used to create dynamic, interactive web pages. It was first created in 1995 by Brendan Eich and is now one of the three core technologies of the World Wide Web, along with HTML and CSS.

Section 2: Why is JavaScript important? JavaScript is important because it allows web developers to create interactive and dynamic websites. With JavaScript, you can create pop-ups, animations, interactive forms, and much more. It is also widely used on the server-side, using frameworks like Node.js, allowing developers to create full-stack web applications.

Section 3: What can you do with JavaScript? JavaScript is a versatile language that can be used for a wide range of tasks. Here are just a few examples:

  • Create interactive user interfaces: JavaScript can be used to create dynamic user interfaces that respond to user input and interactions.
  • Validate user input: JavaScript can be used to validate user input in forms, ensuring that data is entered correctly.
  • Create animations: With JavaScript, you can create animations and visual effects that add an extra layer of interactivity to your web pages.
  • Make API calls: JavaScript can be used to make API calls to retrieve data from servers and display it on a web page.
  • Build web applications: Using frameworks like React, Vue, or Angular, you can use JavaScript to build powerful web applications that run on the client and server side.

Section 4: How do you learn JavaScript? Learning JavaScript is relatively easy, especially if you already have some experience with programming. There are many resources available, including online courses, tutorials, and books. Some popular online resources for learning JavaScript include Codecademy, FreeCodeCamp, and W3Schools.

Conclusion: JavaScript is an essential language for web development, allowing developers to create dynamic and interactive web pages. With the rise of full-stack web development, JavaScript has become even more important, as it is now used on both the client and server sides. If you are interested in web development, learning JavaScript is a great place to start.

AdSense

The Ultimate Guide to Interceptors: Understanding Their Power and Functionality

  The Ultimate Guide to Interceptors: Understanding Their Power and Functionality An interceptor is a service that can intercept HTTP reques...

Follow